Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 8609

MicroPython • Re: Why is struct.unpack_from giving me a bad typecode?

$
0
0
Same error though different text when using desktop Python.

I have not used struct much but it seems the "!" is a formatting character which applies to the whole lot and can only appear at the start of the string, that is, "!HHHH" works as "!H!H!H!H". I couldn't find anywhere that limitation is explicitly described though but didn't look too hard.

Code:

import structdef dump(desc, lst):  o = ""  for n in lst:    o += "0x{:04X} ".format(n)  print(desc, o.strip())  b = b"\x01\x02\x03\x04\x05\x06\x07\x08"print(b)dump("HHHH  =", struct.unpack_from("HHHH", b, 0))dump("!HHHH =", struct.unpack_from("!HHHH", b, 0))

Code:

b'\x01\x02\x03\x04\x05\x06\x07\x08'HHHH  = 0x0201 0x0403 0x0605 0x0807!HHHH = 0x0102 0x0304 0x0506 0x0708
Same using MicroPython as desktop Python.

I guess something like "!HH!HH", hoping to see " 0x0102 0x0403 0x0506 0x0807", isn't possible without splitting things up and doing them separately.

Statistics: Posted by hippy — Sat Sep 06, 2025 1:49 am



Viewing all articles
Browse latest Browse all 8609

Trending Articles