I read a binary file and get an array with characters. When converting two bytes to an integer I do 256*ord(p1) + ord(p0)
. It works fine for positive integers but when I get a negative number it doesn't work. I know there is something with the first bit in the most significant byte but with no success.
I also understand there is something called struct
and after reading I ended up with the following code
import struct
p1 = chr(231)
p0 = chr(174)
a = struct.unpack('h',p0+p1)
print str(a)
a
becomes -6226 and if I swap p0
and p1
I get -20761.
a
should have been -2