Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

About data structure

1 view
Skip to first unread message

malan

unread,
Sep 30, 2002, 3:29:52 PM9/30/02
to
Hi all,

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

Martin v. Loewis

unread,
Sep 30, 2002, 3:39:00 PM9/30/02
to
malan <ma...@club-internet.fr> writes:

> 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

Jon Parise

unread,
Sep 30, 2002, 3:32:24 PM9/30/02
to

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

0 new messages