有时候需要将字符串转换成16进制或者将16进制的转换成字符串格式。下面提供两个函数:
#convert string to hex
def toHex(s):
lst = []
for ch in s:
hv = hex(ord(ch)).replace('0x', '')
if len(hv) == 1:
hv = '0'+hv
lst.append(hv)
return reduce(lambda x,y:x+y, lst)
#convert hex repr to string
def toStr(s):
return s and chr(atoi(s[:2], base=16)) + toStr(s[2:]) or ''
No comments:
Post a Comment