I read the binary 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 that there is something with the first bit in the most significant byte, but without success.
I also understand that there is something called struct
, and after reading it I got 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 exchange p0
and p1
, I get -20761.
a
should be -2
source share