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

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

$
0
0
I am trying to parse a DNS query reply. Since the format is well-defined (RFC1035, among other places), I thought I would have relatively little trouble.

So, starting with the basics, I have a bytes object containing my data (collected by socket.recvfrom).
The first thing in the data is a header, which consists of a 16-bit ID value (which is a known value, since it is my original request id), the next is (effectively) another 16-bit value consisting of groups of flags. This is then followed by four 16-bit values each of which is a count of other entities following the header.

All of these are in network byte order, so I thought that:

Code:

v = struct.unpack_from( '!H!H!H!H!H!H', data, 0)
would give me a tuple containing the values.
The data contains more bytes than are required to unpack according to the given format.

However, instead, I see this:

Code:

ValueError: bad typecode
Now, if I remove the ! which signifies network byte order, I don't get the error, but of course my values are not correct, since the byte-order is wrong.
Do I really have to byte-swap for myself?
It looks like struct.unpack_from just doesn't these byteorder indicators?

Statistics: Posted by SteveSpencer — Fri Sep 05, 2025 2:39 pm — Replies 0 — Views 53



Viewing all articles
Browse latest Browse all 7513

Trending Articles