Jump to content
  • Advertisement
Sign in to follow this  
eedok

Python type question

This topic is 4080 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
Advertisement
You know... the timestamps on your post and your edit say you found the answer 31 minutes before you posted the question!

We really need to sort that out...

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!