代码源于网络:
十六进制转ascii与ascii转十六进制
#!/usr/bin/python
# -*- coding: UTF-8 -*-
class Converter(object):
def to_ascii(h):
list_s = []
for i in range(0,len(h),2):
list_s.append(chr(int(h[i:i+2],16)))
return ''.join(list_s)
def to_hex(s):
list_h = []
for c in s:
list_h.append(str(hex(ord(c))[2:]))
return ''.join(list_h)
print(Converter.to_hex("hello world!"))
print(Converter.to_ascii("68656c6c6f20776f726c6421"))