import binascii
# 16进制字符串 转为 字节
v1 = "4E2918885FD98109869D14E0231A0BF4"
"""
bs = bytearray() # []
for i in range(0, len(v1), 2):
item_hex = v1[i:i + 2]
item_int = int(item_hex, base=16)
bs.append(item_int)
v3 = bytes(bs)
print(v3) # b'N)\x18\x88_\xd9\x81\t\x86\x9d\x14\xe0#\x1a\x0b\xf4'
"""
v3 = binascii.a2b_hex(v1)
print(v3) # b'N)\x18\x88_\xd9\x81\t\x86\x9d\x14\xe0#\x1a\x0b\xf4'
l = [int(i) for i in v3]
print(l) # [78, 41, 24, 136, 95, 217, 129, 9, 134, 157, 20, 224, 35, 26, 11, 244]
#################################################################################
# 数组转换
print(bytearray(l)) # bytearray(b'N)\x18\x88_\xd9\x81\t\x86\x9d\x14\xe0#\x1a\x0b\xf4')
print(bytes(l)) # b'N)\x18\x88_\xd9\x81\t\x86\x9d\x14\xe0#\x1a\x0b\xf4'
l1 = [hex(i) for i in l]
print(l1) # ['0x4e', '0x29', '0x18', '0x88', '0x5f', '0xd9', '0x81', '0x9', '0x86', '0x9d', '0x14', '0xe0', '0x23', '0x1a', '0xb', '0xf4']
print(binascii.b2a_hex(bytearray(l))) # b'4e2918885fd98109869d14e0231a0bf4'
标签:bytearray,进制,16,int,hex,v3,print,xf4,字节
From: https://www.cnblogs.com/hanfe1/p/17195614.html