type converion in python

Ulrich Petri ulope at gmx.de
Wed May 28 19:59:49 EDT 2003


"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






More information about the Python-list mailing list