简体   繁体   English

在python中将int转换为原始数据

[英]Convert int to raw data in python

I'm sure this question has been asked before, but I can't find and answer. 我确定之前已经问过这个问题,但我找不到并回答。

How can I convert an integer to raw data? 如何将整数转换为原始数据?

Here's an example of what I'd like to do, convert 0x41424344 to 'ABCD'? 这是我想做的一个例子,将0x41424344转换为'ABCD'?

I know chr(0x41) = 'A' , but what's the python way of converting an int to a full raw data buffer? 我知道chr(0x41) = 'A' ,但是将int转换为完整原始数据缓冲区的python方法是什么?

Thanks in advance. 提前致谢。

In Python 3: 在Python 3中:

>>> (0x41424344).to_bytes(4, 'big')
b'ABCD'

In Python 2.x - you could use struct : 在Python 2.x中 - 您可以使用struct

>>> struct.pack('>i', 0x41424344)
'ABCD'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM