struct.error: unpack requires a string argument of length 4

前端 未结 2 1321
眼角桃花
眼角桃花 2020-12-15 17:35

Python says I need 4 bytes for a format code of \"BH\":

struct.error: unpack requires a string argument of length 4

Here is the code, I am

2条回答
  •  庸人自扰
    2020-12-15 18:21

    By default, on many platforms the short will be aligned to an offset at a multiple of 2, so there will be a padding byte added after the char.

    To disable this, use: struct.unpack("=BH", data). This will use standard alignment, which doesn't add padding:

    >>> struct.calcsize('=BH')
    3
    

    The = character will use native byte ordering. You can also use < or > instead of = to force little-endian or big-endian byte ordering, respectively.

提交回复
热议问题