Byte oriented data types in python
apt.shansen at gmail.com
Mon Jan 26 02:52:22 CET 2009
> However, as you keep claiming that the struct module is what
> should be used, I must be missing something about the struct
You seem to be focusing overly on the "C Struct" part of the description of
what the struct module does, instead of the part where it says, "packed
binary data" and "This can be used in handling binary data stored in files
from network connections"
The struct module is very useful when generating or interpreting a stream
of binary data which has a set structured format, in bytes and with set
of data types.
> # I have following packet format which I have to send over Bluetooth.
> # packet_type (1 byte unsigned) || packet_length (1 byte unsigned) ||
> # packet_data(variable)
> So yes, all his date is comprised of 8-bit bytes, and yes, he doesn't
> need the struct module. Hence I'm puzzled why people suggest that
> he uses the struct module.
> I think the key answer is "use the string type, it is appropriate
> to represent byte oriented data in python" (also see the subject
> of this thread)
Everyone *is* saying to use the string type: the struct module doesn't
structs.. it produces strings. The question is how to convert from Python
to byte-representations of said types in a string: you are saying (for
for shorts someone should use chr(1) etc. What if he needs to then send a
byte int from a Python int?
Just because he said his message was about a fairly simple requirement at
the moment, it doesn't mean that's the end of his problem. Everyone else
seems to have assumed that it'll advance as he supports more of this
protocol: that <type><length><variable data> is apt to take on more specific
struct format messages based on <type>. So you might have a connection
request format that's pre-defined -- your example above -- or another which
needs to pass say, a 2 byte int instead.
The struct module easily accomplishes this: all together in one place.
of having to figure out how to calculate the 2 byte storage of an int down
road, or a 4 byte integer, etc. Not to mention endianness.
If the OP never ever has to do anything but the one specific problem he's
right now (and this isn't just an example he's making) with really really
data: two byte-ints and then some characters, then sure using chr() and
concatenation is probably fine. But struct works -perfectly- fine for
and as he extends his support for the protocol, is quite likely to be very
to him: that's why we're suggesting it. IMHO.
I'd rather spend some trival time using string concatination (or the %
generating format strings for struct then have to write code for
and "int_to_4byte_int" and "change_to_bigendian" for when chr() doesn't
the conversation needs enough.
Yeah, strings are exactly how he can and should store the byte oriented data
in Python 2.x. Using concatination and slicing + conversion to read and
byte stream coming off a line protocol... if you want to. But there's tools
there that make it all alot easier too. The struct module for example.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Python-list