• Advertisement
Sign in to follow this  

Python type question

This topic is 3990 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);
  return 0;

and then python reads the file:
>>> myfile = open("myfile.txt","rb")
>>> myfile.read()

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]

[Edited by - eedok on April 20, 2007 5:19:25 PM]

Share this post

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

  • Advertisement