Reading 3 bytes as an integer

前端 未结 4 754
执笔经年
执笔经年 2021-02-13 10:16

How can I read 3 bytes as an integer?

Does struct module provide something like that?

I can read in 3 bytes and add an extra \\x00 and then interpret it as a 4-b

4条回答
  •  逝去的感伤
    2021-02-13 11:07

    The struct module has no option for 3-byte integers, so I think your idea of appending '\x00' is the easiest way.

    In [30]: import struct
    In [38]: struct.pack('>3b',0,0,1)
    Out[38]: '\x00\x00\x01'
    
    In [39]: struct.unpack('>i','\x00'+'\x00\x00\x01')
    Out[39]: (1,)
    

提交回复
热议问题