Reading 3 bytes as an integer

前端 未结 4 734
执笔经年
执笔经年 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:04

    Another option is to just do the bit shifting yourself.

    Big Endian:

    ba = # 3 byte bytearray
    v = ba[0] << 16 | ba[1] << 8 | ba[2]
    

    Little Endian

    ba = # 3 byte bytearray
    v = ba[2] << 16 | ba[1] << 8 | ba[0]
    

提交回复
热议问题