简体   繁体   English

将十六进制的ASCII转换为python中的字符串

[英]Convert ASCII in hex to string in python

I use windows 7 and python 2.7. 我使用Windows 7和python 2.7。 I want to convert sting: '['3732', '3130', '2039', '6638', '6420', '3765', '6632']' , which stands for the ASCII in hex of '7210 9f8d 7ef2'(including blank space) to str: '7210 9f8d 7ef2' 我要转换字符串: '['3732', '3130', '2039', '6638', '6420', '3765', '6632']' ,它代表十六进制的ASCII字符'7210 9f8d 7ef2 '(包括空格)到str: '7210 9f8d 7ef2'

(The ASCII of 7 in hex is 37, 2 is 32, blank space is 20) (十六进制的7的ASCII是37,2是32,空格是20)

I have tried: 我努力了:

f = ['3732', '3130', '2039', '6638', '6420', '3765', '6632']
 g = []
 for i in f:

   g.append(i.decode("hex"))

print str(g).replace(', ', ' ').replace('\'', '')

but the result is [72 10 9 f8 d 7e f2] 但结果是[72 10 9 f8 d 7e f2]

What you need is to use ''.join() . 您需要使用''.join() So, rather than using: 因此,而不是使用:

print str(g).replace(', ', ' ').replace('\'', '')

replace this: 替换为:

print ''.join(g)

Or you can replace all your code with: 或者,您可以将所有代码替换为:

f = ['3732', '3130', '2039', '6638', '6420', '3765', '6632']
print ''.join([i.decode("hex") for i in f])

The output would be: 输出为:

'7210 9f8d 7ef2' '7210 9f8d 7ef2'

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

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