Byte oriented data types in python

Stephen Hansen apt.shansen at
Sat Jan 24 20:07:32 CET 2009

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)
> How to construct these using python data types, as int and float have
> no limits and their sizes are not well defined.

Check out the struct module.

You want something like:

  data = struct.pack("BB4s", 1, 4, "this")

It returns a string of bytes according to the specification -- B is unsigned
byte, and "4s" is a string of 4 characters. The 1 feeds into the first byte,
the 4 into the second, etc.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the Python-list mailing list