且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Python中存储十六进制并将十六进制转换为ASCII?

更新时间:2022-11-16 15:05:48

int( 0x31,16)部分正确:

>>> int("0x31",16)
49

但是要将其转换为字符,您应该使用 chr(...)函数代替:

But to convert that to a character, you should use the chr(...) function instead:

>>> chr(49)
'1'

将它们放在一起(在第一个字母上) ):

Putting both of them together (on the first letter):

>>> chr(int("0x53", 16))
'S'

整个列表:

>>> [chr(int(i, 16)) for i in "0x53 0x48 0x41 0x53 0x48 0x49".split()]
['S', 'H', 'A', 'S', 'H', 'I']

最后将其转换为字符串:

And finally turning it into a string:

>>> hex_string = "0x53 0x48 0x41 0x53 0x48 0x49"
>>> ''.join(chr(int(i, 16)) for i in hex_string.split())
'SHASHI'

我希望这会有所帮助!