Python 字符串16进制转换为字符串

介绍

我们知道,base16编码就是将字符用16进制表示

’a’ =base16=> b’61’

那么简单对16进制字符串进行base16解码即可

binascii方法:

import binascii

hex_str = "557365723a20746573740d0a50617373776f72643a206f7073313233210d0a"

hex = hex_str.encode('utf-8')
str_bin = binascii.unhexlify(hex)
str = str_bin.decode('utf-8')

print str

base16方法:

hex_str = "557365723a20746573740d0a50617373776f72643a206f7073313233210d0a"

import base64

print(base64.b16decode(hex_str.upper()))

 

 

直接转换:

hex_str = "557365723a20746573740d0a50617373776f72643a206f7073313233210d0a"
b_list = re.findall(".{2}", hex_str)
new_b = "\\x" + "\\x".join(b_list)
print new_b.decode("raw_unicode_escape")

 注:此方法暂时是有问题的,直接打印不是明文,但是复制后在打印就是明文。原因未知(持续更新)。

posted @ 2021-01-28 21:16  你的小可爱吖  阅读(2937)  评论(0编辑  收藏  举报