如何将int转换为24bit字符串?

时间:2011-08-03 09:11:07

标签: python

供我阅读:

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

和写作?

2 个答案:

答案 0 :(得分:5)

更简单地填充它们并作为长期处理

>>> from struct import pack, unpack
>>> def unpack24(s):
...     return unpack(">L","\0"+s)[0]
... 
>>> def pack24(i):
...     return pack(">L",i)[1:]
... 

答案 1 :(得分:0)

未经测试!

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