您好,欢迎来到知库网。
搜索
您的当前位置:首页16进制字符串转化为bytes数组

16进制字符串转化为bytes数组

来源:知库网

代码:

# -*- 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))

其中第一个和第二个基本是一样的, 所有三个大同小异

Copyright © 2019- zicool.com 版权所有 湘ICP备2023022495号-2

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务