2's complement conversion. Is this right?

Ross Ridge rridge at caffeine.csclub.uwaterloo.ca
Fri Apr 18 16:37:21 EDT 2008


Bob Greschke  <bob at passcal.nmt.edu> wrote:
>I'm reading 3-byte numbers from a file and they are signed (+8 to 
>-8million).  This seems to work, but I'm not sure it's right.
>
># Convert the 3-characters into a number.
>    Value1, Value2, Value3 = unpack(">BBB", Buffer[s:s+3])
>    Value = (Value1*65536)+(Value2*256)+Value3
>        if Value >= 0x800000:
>            Value -= 0x1000000
>    print Value
>
>For example:
>16682720 = -94496
>
>Should it be Value -= 0x1000001 so that I get -94497, instead?

Your first case is correct, "Value -= 0x1000000".  The value 0xFFFFFFF
should be -1 and 0xFFFFFFF - 0x1000000 == -1.

An alternative way of doing this:

	Value = unpack(">l", Buffer[s:s+3] + "\0")[0] >> 8

					Ross Ridge

-- 
 l/  //	  Ross Ridge -- The Great HTMU
[oo][oo]  rridge at csclub.uwaterloo.ca
-()-/()/  http://www.csclub.uwaterloo.ca/~rridge/ 
 db  //	  



More information about the Python-list mailing list