Guaranteeing an n-byte data type?

Grant Edwards invalid at invalid
Fri Jan 9 17:56:44 CET 2009


Evan Jones wrote:

> I'm trying to use sockets to implement a pre-defined network
> protocol that requires that I send messages of exactly a
> certain number of bytes.  In Python, integer values are
> represented as 4 bytes each (AFAIK.) However I don't want to
> always send 4 bytes: sometimes I want to send one byte, or 11
> bytes, or 33 bytes, or any other permutation that's not a
> multiple of 4. It seems to make the most sense to use one-byte
> data members and concatenate them before sending.

In addition to previous suggestions, you should look at the
"struct" and "ctypes" modules in the standard Python library.
Both allow you to pack/unpack data to/from data structures with
arbitrary layout and endianness.

-- 
Grant Edwards                   grante             Yow! I'm meditating on
                                  at               the FORMALDEHYDE and the
                               visi.com            ASBESTOS leaking into my
                                                   PERSONAL SPACE!!



More information about the Python-list mailing list