Byte oriented data types in python
apt.shansen at gmail.com
Sun Jan 25 20:32:40 CET 2009
On Sun, Jan 25, 2009 at 7:27 AM, Ravi <ra.ravi.rav at gmail.com> wrote:
> > Take a look at the struct and ctypes modules.
> struct is really not the choice. it returns an expanded string of the
> data and this means larger latency over bluetooth.
Noo... struct really IS the choice; that is the explicit purpose of the
struct library. I went and included an example too which you're not noticing
Python 2.5.1 (r251:54869, Apr 18 2007, 22:08:04)
[GCC 4.0.1 (Apple Computer, Inc. build 5367)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import struct
>>> data = struct.pack("BB4s", 1, 4, "this")
That's precisely six raw bytes which is exactly what you specified you
needed: exactly one unsigned byte for packet type, one unsigned byte for
length, and four bytes for the data after. In real life you'd probably use
something besides "4s" to pack what you marked as "other" and "variable", of
course, but still. Replace it with whatever your message requires.
Really, this is how you do line protocols ;) Well there's other ways, but...
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Python-list