Byte oriented data types in python

Grant Edwards grante at
Mon Jan 26 00:48:59 CET 2009

On 2009-01-25, Martin v. Löwis <martin at> wrote:
>>> Unfortunately, that does not work in the example. We have
>>> a message type (an integer), and a variable-length string.
>>> So how do you compute the struct format for that?
>> I'm confused. Are you asking for an introductory tutorial on
>> programming in Python?
> Perhaps. I honestly do not know how to deal with variable-sized
> strings in the struct module in a reasonable way, and thus believe
> that this module is incapable of actually supporting them
> (unless you use inappropriate trickery).

It deals with variable sized fields just fine:

dtype = 18
dlength = 32
format = "!BB%ds" % dlength

rawdata = struct.pack(format, (dtype,dlength,data))

> However, as you keep claiming that the struct module is what
> should be used, I must be missing something about the struct
> module.

>> I don't understand your point.
>>> py> payload="call me"
>>> py> encode(CONNECT_REQUEST, len(payload), payload)
>>> '\x11\x07call me'
>> If all your data is comprised of 8-bit bytes, then you don't
>> need the struct module.
> Go back to the original message of the OP. It says
> #  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,

He doesn't specify what format the packet_data is, and we all
assumed he needed to handle conversion of various data types
to/from raw byte-strings.

> and yes, he doesn't need the struct module. Hence I'm puzzled
> why people suggest that he uses the struct module.

We all assumed that "packet_data" might contain values of
various types such as 16 or 32 bit integers, floating point
values -- that packet_data was not solely arbitrary-length
strings of 8-bit bytes.

> 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)

I, for one, interpreted "byte-oriented" to mean that the data
was received/sent as blocks of bytes but needed to be converted
into other data types.  If the data really is just strings of
bytes, and it's sent as strings of bytes, then I have no idea
what the OP was asking, since there's nothing that needs to be
done with the data.


More information about the Python-list mailing list