Reading 3 bytes as an integer

前端 未结 4 736
执笔经年
执笔经年 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 10:44

    I think from 3.2, int developed a new method .from_bytes, so you're able to use the following instead of struct.unpack:

    int.from_bytes(b'\x00\x00\x01', 'big')  
    # 1
    

    For reference, see: http://docs.python.org/dev/library/stdtypes.html#int.from_bytes

提交回复
热议问题