1. 程式人生 > >python 字符串與16進制 轉化

python 字符串與16進制 轉化

x64 urn abcdefg div pri 字符串 ogr print ret

def str_to_hex(s):
    return r"/x"+r/x.join([hex(ord(c)).replace(0x, ‘‘) for c in s])

def hex_to_str(s):
    return ‘‘.join([chr(i) for i in [int(b, 16) for b in s.split(r/x)[1:]]])
    
def str_to_bin(s):
    return  .join([bin(ord(c)).replace(0b, ‘‘) for c in s])
    
def bin_to_str(s):
    
return ‘‘.join([chr(i) for i in [int(b, 2) for b in s.split( )]]) a="abcdefg" x=str_to_hex(a) print(x) print(hex_to_str(x))

輸出:

/x61/x62/x63/x64/x65/x66/x67
abcdefg                                                     
[Program finished]

demo2

def str_to_hex(s):
    return  .join([hex(ord(c)).replace(
0x, ‘‘) for c in s]) def hex_to_str(s): return ‘‘.join([chr(i) for i in [int(b, 16) for b in s.split( )]]) def str_to_bin(s): return .join([bin(ord(c)).replace(0b, ‘‘) for c in s]) def bin_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))

輸出

61 62 63 64 65 66
abcdef                                                    
[Program finished]

python 字符串與16進制 轉化