Python 中浮点数的主机到网络字节顺序转换
我有这段代码,我想为浮点值(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))
注意:这些参数的值位于另一个文本文件中,我从那里获取这些值,就像这样
./python.py `cat args-file` > /somewhere
我真的是初学者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 text 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
struct
模块支持格式字符串中的字节订单指定器: http://docs.python.org/library/struct.html#byte-order-corder-size-size-and-alignmentThe
struct
module supports byte order specifiers in the format string: http://docs.python.org/library/struct.html#byte-order-size-and-alignment