defstr_to_hex(s): return' '.join([hex(ord(c)).replace('0x', '') for c in s])
defhex_to_str(s): return''.join([chr(i) for i in [int(b, 16) for b in s.split(' ')]]) defstr_to_bin(s): return' '.join([bin(ord(c)).replace('0b', '') for c in s]) defbin_to_str(s): return''.join([chr(i) for i in [int(b, 2) for b in s.split(' ')]]) a="abcdef" x=str_to_hex(a) print(x) print(hex_to_str(x))
不带0x的连续十六进制字符串转换:
1 2 3 4 5 6 7 8 9 10 11 12 13
import re
defhex_to_str(s): pattern = re.compile(r'[A-Z0-9]{2}') return''.join([chr(i) for i in [int('0x'+str(b), 16) for b in pattern.findall(s) ]])
#raw="" #with open('raw','r') as f: # raw=f.readlines() #raw=str(raw) raw="3C3F7068700A24666C6167203D2027666C61677B66396361316136622D396437382D313165382D393061332D6334623330316237623939627D273B0A3F3E0A" print(hex_to_str(raw))