I need to build a data structure like this :
size 4 bytes ( example 0x0000000A )
flag 1 byte ( example 0x87 )
trans 8 bytes ( example 'URTVMI1' )
F5 3 bytes ( example 0x00125C )
How can I define that structure, format it and send it via a socket ?
My problem is to code / decode the message to and from the server.
malan
> I need to build a data structure like this :
>
> size 4 bytes ( example 0x0000000A )
> flag 1 byte ( example 0x87 )
> trans 8 bytes ( example 'URTVMI1' )
> F5 3 bytes ( example 0x00125C )
>
> How can I define that structure, format it and send it via a socket ?
Your specification is incomplete, as you don't give the byte
order. Assuming it has to be big-endian, that would be
>>> import struct
>>> struct.pack(">IB8s3b", 0xA, 0x87, "URTVMI1", 0x0, 0x12, 0x5c)
'\x00\x00\x00\n\x87URTVMI1\x00\x00\x12\\'
For details, help(struct).
HTH,
Martin
You want to use the 'struct' module:
http://www.python.org/doc/current/lib/module-struct.html
--
Jon Parise (j...@csh.rit.edu) . Information Technology (2001)
http://www.csh.rit.edu/~jon/ : Computer Science House Member