type converion in python

Winnie Poon wpoon at generalhydrogen.com
Thu May 29 19:57:43 EDT 2003


Hi,

Thanks everyone for your inputs.  I actually wasn't thinking about
writing it from scratch. :)  I'm still new to this python language, so
just didn't realize there's a struct module with pack/unpack to do the
job for me.  I've solved my problem with this struct module! Thanks
again for your help.

Winnie :)

-----Original Message-----
From: Ulrich Petri [mailto:ulope at gmx.de] 
Sent: Wednesday, May 28, 2003 5:00 PM
To: python-list at python.org
Subject: Re: type converion in python


"Winnie Poon" <wpoon at generalhydrogen.com> schrieb im Newsbeitrag
news:mailman.1054154142.27449.python-list at python.org...
> Hi,
>
> I'm writing a socket server in python and I'm having some trouble
converting the data received.
>
> Another socket client, which is written in C, sends data to a
particular
ipaddress and port number and my job is to parse the > data received.
Now
the problem is, the data sent from the client is an array of uint2. If
the
server is written also in C/C++,
> then I can cast the char* to uint2 * and i would be able to obtain the
data.
>
> char buf[MAXRECV+1];
> uint2 * in_ptr = 0;
>
> Msglen = recv(sock, buf, MAXRECV, 0)
>
> in_ptr = (uint2*) (&buf[0]);
>
> Now with python, I have trouble doing similar thing.  Anybody has done
this before? Any help is very much appreciated.
> Thanks!

once you have the data as a python string you can use it in any way you
like
e.g.:

# i'm assuming buf is buffer with data
#i dont know how much bytes a uint2 has....
bytes_per_uint = 2

list_of_ints = [int(buf[i:i+bytes_per_uint]) for i in
xrange(len(buf)/bytes_per_uint)]


HTH

Ciao Ulrich


-- 
http://mail.python.org/mailman/listinfo/python-list





More information about the Python-list mailing list