代码:
# -*- coding: utf-8 -*-
from binascii import a2b_hex
def hex2bin_1(s):
ss = []
# 按照2个一组转化为str数组
ss = [s[i:i+2] for i in range(0, len(s), 2)]
return bytes(bytearray(int(x, 16) for x in ss))
def hex2bin_2(s):
return bytes(bytearray.fromhex(s))
def hex2bin_3(s):
return a2b_hex(s)
if __name__ == '__main__':
s = '1ab2'
print(hex2bin_1(s))
print(hex2bin_2(s))
print(hex2bin_3(s))
其中第一个和第二个基本是一样的, 所有三个大同小异