2

for read I use:

def UI24(t):
    result = 0
    for i in xrange(3):
        result = (result << 8);
        byte = unpack('>b',t[i-1])
        result += byte;
    return result

and for write ?

Emil Ivanov
  • 37,300
  • 12
  • 75
  • 90
bdfy
  • 285
  • 2
  • 5
  • 7

2 Answers2

5

Simpler to just pad them and treat as longs

>>> from struct import pack, unpack
>>> def unpack24(s):
...     return unpack(">L","\0"+s)[0]
... 
>>> def pack24(i):
...     return pack(">L",i)[1:]
... 
John La Rooy
  • 295,403
  • 53
  • 369
  • 502
0

This is not tested !

def UI24back(value):
    result = ""
    for i in xrange(3):
        result = pack('>b', value &255) + result
        value  >>= 8
    return result
rocksportrocker
  • 7,251
  • 2
  • 31
  • 48