开发者

How to convert int to 24bit string?

开发者 https://www.devze.com 2023-03-25 02:30 出处:网络
for read I use: d开发者_Go百科ef UI24(t): result = 0 for i in xrange(3): result = (result << 8);

for read I use:

d开发者_Go百科ef 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 ?


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:]
... 


This is not tested !

def UI24back(value):
    result = ""
    for i in xrange(3):
        result = pack('>b', value &255) + result
        value  >>= 8
    return result
0

精彩评论

暂无评论...
验证码 换一张
取 消