本文介绍了将 Python int 转换为 big-endian 字节字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个非负整数,我想有效地将它转换为包含相同数据的大端字符串.例如,int 1245427(即 0x1300F3)应生成一个长度为 3 的字符串,其中包含三个字节值为 0x13、0x00 和 0xf3 的字符.
I have a non-negative int and I would like to efficiently convert it to a big-endian string containing the same data. For example, the int 1245427 (which is 0x1300F3) should result in a string of length 3 containing three characters whose byte values are 0x13, 0x00, and 0xf3.
我的整数是 35(基数为 10)位的数字.
My ints are on the scale of 35 (base-10) digits.
我该怎么做?
推荐答案
您可以使用 struct 模块:
import struct
print struct.pack('>I', your_int)
'>I'
是格式字符串.>
表示大端,I
表示无符号整数.查看文档以获取更多格式字符.
'>I'
is a format string. >
means big endian and I
means unsigned int. Check the documentation for more format chars.
这篇关于将 Python int 转换为 big-endian 字节字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!