简体   繁体   English

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

[英]How to convert int to 24bit string?

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 ? 和写?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM