say I'm writing a client/server pair, and using python for the server and another strongly typed language for the client.
now if the client were to send this:
short num=24931;
socket.send(num);
python receives it like so
num = socket.recv(1024)
print num
num comes up as ac(or ca depending on the byte ordering), is there a way to make python recognize this is a short and not a string?
EDIT:another example except with files instead of sockets
C++ writes this:
#include <iostream>
#include <fstream>
using namespace std;
int main () {
ofstream myfile;
short num = 24931;
myfile.open ("myfile.txt");
myfile.write((char *)&num,2);
myfile.close();
return 0;
}
and then python reads the file:
>>> myfile = open("myfile.txt","rb")
>>> myfile.read()
'ca'
how do I make the result of myfile.read() to a short so I can get 24931 like I put into it?
EDIT2: found the answer
>>> myfile = open("myfile.txt","rb")
>>> data=myfile.read()
>>> import struct
>>> struct.unpack("h",data)[0]
24931
[Edited by - eedok on April 20, 2007 5:19:25 PM]