Bob Greschke <[EMAIL PROTECTED]> 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] [EMAIL PROTECTED] -()-/()/ http://www.csclub.uwaterloo.ca/~rridge/ db // -- http://mail.python.org/mailman/listinfo/python-list