[英]Convert hex-string to string using binascii
By hex-string, it is a regular string except every two characters represents some byte, which is mapped to some ASCII char. 用十六进制字符串表示,它是一个常规字符串,但每两个字符表示一个字节,该字节映射到某个ASCII字符。
So for example the string 因此,例如字符串
abc
Would be represented as 将表示为
979899
I am looking at the binascii module but don't really know how to take the hex-string and turn it back into the ascii string. 我正在看binascii模块,但真的不知道如何获取十六进制字符串并将其转换回ascii字符串。 Which method can I use? 我可以使用哪种方法?
Note: I am starting with 979899
and want to convert it back to abc
注意:我从979899
开始,想将其转换回abc
You can use ord()
to get the integer value of each character: 您可以使用ord()
获得每个字符的整数值:
>>> map(ord, 'abc')
[97, 98, 99]
>>> ''.join(map(lambda c: str(ord(c)), 'asd'))
'979899'
>>> ''.join((str(ord(c)) for c in 'abc'))
'979899'
You don't need binascii to get the integer representation of a character in a string, all you need is the built in function ord()
. 您不需要binascii即可获取字符串中字符的整数表示,您所需要的只是内置函数ord()
。
s = 'abc'
print(''.join(map(lambda x:str(ord(x)),s))) # outputs "979899"
To get the string back from the hexadecimal number you can use 要从十六进制数取回字符串,可以使用
s=str(616263)
print "".join([chr(int(s[x:x+2], 16)) for x in range(0,len(s),2)])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.