问题描述
我希望将一个整数(它将
例如:我想传入 65
,然后出去'\x41'
或 255
code>'\xff'。
我试着用 struct.pack ('c',
65 )
,但是对于任何高于 9
希望采用单个字符串。
您正在寻找 chr
函数。
您似乎混合了整数和十六进制整数的十进制表示,所以它不完全清楚您需要什么。根据您给出的描述,我认为其中的一个片段可以显示您想要的内容。
>>> chr(0x65)=='\x65'
True
>>>十六进制(65)
'0x41'
>>> chr(65)=='\x41'
True
请注意,不同于包含十六进制整数的字符串。如果这是你想要的,使用十六进制
内置。
I want to take an integer (that will be <= 255), to a hex string representation
e.g.: I want to pass in 65
and get out '\x41'
, or 255
and get '\xff'
.
I've tried doing this with the struct.pack('c',
65)
, but that chokes on anything above 9
since it wants to take in a single character string.
You are looking for the chr
function.
You seem to be mixing decimal representations of integers and hex representations of integers, so it's not entirely clear what you need. Based on the description you gave, I think one of these snippets shows what you want.
>>> chr(0x65) == '\x65'
True
>>> hex(65)
'0x41'
>>> chr(65) == '\x41'
True
Note that this is quite different from a string containing an integer as hex. If that is what you want, use the hex
builtin.
这篇关于如何将int转换为十六进制字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!