[BangPypers] pack data

Anand Balachandran Pillai abpillai at gmail.com
Fri May 28 10:42:46 CEST 2010


On Fri, May 28, 2010 at 12:56 PM, Noufal Ibrahim <noufal at gmail.com> wrote:

> On Fri, May 28, 2010 at 12:49 PM, murugadoss <murugadoss2884 at gmail.com>
> wrote:
> > Hello all,
> >
> > I am trying a sample program for send set of data [ kept in array]. I
> have
> > attached the code below,
> >
> > import struct
> >
> > sendvalues = [181, 98, 11, 01]
> > for i in range(4):
> > snddata = struct.pack('int',int(sendvalue[i]))
> > sendto(sndData,("localhost"))
>

Just do this.

>>> l=[181, 98, 11, 01]
>>> b=struct.pack('H'*4, *l)
>>> b
'\xb5\x00b\x00\x0b\x00\x01\x00'
>>> struct.unpack('H'*4,b)
(181, 98, 11, 1)

Since your integers are small you can use "unsigned short"
for which the specifier is 'H'. If you have any doubts, ask.




-- 
--Anand


More information about the BangPypers mailing list