python工具-hex2str str2hex

python工具-hex2str str2hex

## str2hex hex2str

import sys

def str2hex(input_data):
    by = bytes(input_data,'UTF-8')
    return by.hex()

def hex2str(hex_string):
    byte_string = bytes.fromhex(hex_string)            
    result = byte_string.decode('utf-8')
    return result

if __name__ == "__main__":
    for i in range(len(sys.argv)-1):
        hex = str2hex(sys.argv[i+1])
        print(hex)
        str_data = hex2str(hex)
        print(str_data)

# python str2hex.py hello
# 68656c6c6f
# hello

你可能感兴趣的:(工具类,python,开发语言)