2's complement conversion. Is this right?

Bob Greschke bob at passcal.nmt.edu
Fri Apr 18 15:57:56 EDT 2008


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?

Thanks!

Bob




More information about the Python-list mailing list