Host to network byteorder conversion for float in Python
I have this code and I want to do host to network for the float values (t & u)
#! /usr/bin/env python
import socket
import sys, struct
x = int(sys.argv[1])
y = int(sys.argv[2])
z = socket.htons(int(sys.argv[3]))
t = float(sys.argv[4]) # I want to do (socket.htonf) or something similar but only (htonl) is there
u = float(sys.argv[5])
sys.stdout.write(struct.pack('BBhff',x,y,z,t,u))
note: the values for these arguments are in another tex开发者_开发技巧t file, and I get these values from there like this
./python.py `cat args-file` > /somewhere
I'm really beginner in Python, any help is really appreciated.
Thank you all
The struct
module supports byte order specifiers in the format string: http://docs.python.org/library/struct.html#byte-order-size-and-alignment
精彩评论