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

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

$
0
0
This refers to a subset of CPython's struct:
https://docs.micropython.org/en/latest/ ... ule-struct

But the doc does not explain, that the byte order could be only the first character in the format string.

CPython-Docs: https://docs.python.org/3/library/struc ... -alignment
Alternatively, the first character of the format string can be used to indicate the byte order, size and alignment of the packed data, according to the following table:
Furthermore, if you scroll down, you'll find following:
A format character may be preceded by an integral repeat count. For example, the format string '4h' means exactly the same as 'hhhh'.
This means in your case, you could just use "!6H" as format:

Code:

[deadeye@nexus build-standard]$ ./micropython MicroPython v1.27.0-preview.107.gd1607598f.dirty on 2025-09-09; linux [GCC 15.2.1] versionUse Ctrl-D to exit, Ctrl-E for paste mode>>> import struct>>> struct.unpack("!6H", bytearray([0]*12), 0)(0, 0, 0, 0, 0, 0)>>> 

Statistics: Posted by DeaD_EyE — Tue Sep 09, 2025 7:06 pm



Viewing all articles
Browse latest Browse all 8042

Trending Articles