Byte oriented data types in python

"Martin v. Löwis" martin at v.loewis.de
Mon Jan 26 00:36:51 CET 2009


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

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> CONNECT_REQUEST=17
>> 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, 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)

Regards,
Martin



More information about the Python-list mailing list