开发者

Host to network byteorder conversion for float in Python

开发者 https://www.devze.com 2023-04-06 02:52 出处:网络
I have this code and I want to do host to network for the float values (t & u) #! /usr/bin/env 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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号