十六进制 ascii码 字符串
def is_hex(string):
hex_chars = set('0123456789abcdefABCDEF')
return all(c in hex_chars for c in string)
def is_ascii(string):
return all(ord(c) < 128 for c in string)
import binascii
str_bytes = b'3010864E725979D16280301160A86B635728767B5F55864E725976F464ADFF0C9A8C8BC17801003500350030003900370036FF0C003100305206949F51856709654830026E2999A863D0793AFF1A5B9865B94E0D4F1A541160A87D2289816B644FE1606FFF0C8BF752FF544A77E54ED64EBAFF0C8C2896328D2653F788AB76D73002'
# 将16进制字符串转换为字节串
bytes_data = binascii.unhexlify(str_bytes)
# 将字节串转换为文本字符串
str_correct = bytes_data.decode('ascii', errors='ignore')
print(str_correct)
list_strip = [i.strip() for i in str_result]
list_result = []
for i in list_strip:
# 判断是否为十六进制,且非空
if (is_hex(i.decode())) and (len(i) > 0):
print(i, '\n')
decode_i = i.decode('utf-8')
bytes_i = bytes.fromhex(decode_i)
text_i = bytes_i.decode('ascii', errors='ignore')
print(text_i, '\n')
标签:十六进制,string,hex,bytes,decode,str,字符串,ascii
From: https://blog.51cto.com/u_16055028/7075416