怎么把16进制的ASC码转换成字符串的显示,用python实现

2024-11-08 11:51:00
推荐回答(1个)
回答(1):

#将16进制字符转成二进制字符
def str2byte(s):
base='0123456789ABCDEF'
i=0
s = s.upper()
s1=''
while i < len(s):
c1=s[i]
c2=s[i+1]
i+=2
b1=base.find(c1)
b2=base.find(c2)
if b1 == -1 or b2 == -1:
return None
s1+=chr((b1 <宏数凯<蔽唤 4)+b2)
return s1

s = 'C7 EB CE F0 BE C6 BA F3 BC DD B3 B5'.replace(' ','') #去掉空格
s1 = str2byte(s)
print s1.decode('gbk') #以gbk编码解码输出

#结果
>>> s1
'\xc7\xeb\xce\xf0\xbe\xc6\毕塌xba\xf3\xbc\xdd\xb3\xb5'
>>> print s1.decode('gbk')
请勿酒后驾车