[英]How to convert a number in Python to 8 binary bytes (64-bit long)?
How do I convert a number in Python to 8 binary bytes (64-bit long)? 如何将Python中的数字转换为8个二进制字节(64位长)?
I have a blueprint of a network message, part of which is a number represented with eight bytes. 我有一个网络消息的蓝图,其中一部分是用八个字节表示的数字。
Use struct.pack : 使用struct.pack :
>>> import struct
>>> struct.pack('!Q', 123)
'\x00\x00\x00\x00\x00\x00\x00{'
The first argument is a format string which controls the encoding. 第一个参数是控制编码的格式字符串 。 !
means network byte order and Q
is for 8-byte unsigned integers. 表示网络字节顺序, Q
表示8字节无符号整数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.