十六进制字符串在python中的字符 [英] hex string to character in python
本文介绍了十六进制字符串在python中的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
data =437c2123
我想根据ASCII表格将此字符串转换为一系列字符。
结果应该如下所示:
data_con =C |!#
$ c
$ b 任何人都可以告诉我如何做到这一点?
解决方案在[17]中:data =437c2123
在[18]中:''.join(chr(int(data [i: (0,len(data),2))
Out [18]:'C |!#'
这里:
-
范围(0,len(data),2)遍历 data
中的每个第二个位置: 0
, 2
, 4
等。
-
data [i:i + 2]
查看每一对十六进制数字 '43'
,'7c'
等。
-
chr(int(...,16))
将数字合并到相应的字符中。 -
-
-
>
I have a hex string like:
data = "437c2123"
I want to convert this string to a sequence of characters according to the ASCII table.
The result should be like:
data_con = "C|!#"
Can anyone tell me how to do this?
解决方案 In [17]: data = "437c2123"
In [18]: ''.join(chr(int(data[i:i+2], 16)) for i in range(0, len(data), 2))
Out[18]: 'C|!#'
Here:
for i in range(0, len(data), 2)
iterates over every second position in data
: 0
, 2
, 4
etc.
data[i:i+2]
looks at every pair of hex digits '43'
, '7c'
, etc.
chr(int(..., 16))
converts the pair of hex digits into the corresponding character.
''.join(...)
merges the characters into a single string.
这篇关于十六进制字符串在python中的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文