字节数组转十六进制字符串

109

我有一个存储在字节数组中的数据。如何将这些数据转换为十六进制字符串?

我的字节数组示例:

array_alpha = [ 133, 53, 234, 241 ]

5个回答

163

使用str.format

>>> array_alpha = [ 133, 53, 234, 241 ]
>>> print ''.join('{:02x}'.format(x) for x in array_alpha)
8535eaf1

或者使用format

>>> print ''.join(format(x, '02x') for x in array_alpha)
8535eaf1

注意:在格式语句中,02 表示如果需要的话会填充最多 2 个前导的 0。这很重要,因为 [0x1, 0x1, 0x1] 即 (0x010101) 将被格式化为 "111" 而不是 "010101"

或者使用bytearraybinascii.hexlify

>>> import binascii
>>> binascii.hexlify(bytearray(array_alpha))
'8535eaf1'

这里是Python 3.6.1中上述方法的性能基准测试结果:


from timeit import timeit
import binascii

number = 10000

def using_str_format() -> str:
    return "".join("{:02x}".format(x) for x in test_obj)

def using_format() -> str:
    return "".join(format(x, "02x") for x in test_obj)

def using_hexlify() -> str:
    return binascii.hexlify(bytearray(test_obj)).decode('ascii')

def do_test():
    print("Testing with {}-byte {}:".format(len(test_obj), test_obj.__class__.__name__))
    if using_str_format() != using_format() != using_hexlify():
        raise RuntimeError("Results are not the same")

    print("Using str.format       -> " + str(timeit(using_str_format, number=number)))
    print("Using format           -> " + str(timeit(using_format, number=number)))
    print("Using binascii.hexlify -> " + str(timeit(using_hexlify, number=number)))

test_obj = bytes([i for i in range(255)])
do_test()

test_obj = bytearray([i for i in range(255)])
do_test()

结果:

Testing with 255-byte bytes:
Using str.format       -> 1.459474583090427
Using format           -> 1.5809937679100738
Using binascii.hexlify -> 0.014521426401399307
Testing with 255-byte bytearray:
Using str.format       -> 1.443447684109402
Using format           -> 1.5608712609513171
Using binascii.hexlify -> 0.014114164661833684

format 方法提供了额外的格式选项,例如用空格 " ".join 或逗号 ", ".join 分隔数字,以及大写打印 "{:02X}".format(x)/format(x, "02X") 等。但这些操作会带来极大的性能影响。


1
你上次的操作在我的系统上返回了b'8535eaf1',这里的 b 代表什么? - Grijesh Chauhan
1
@GrijeshChauhan,你在使用Python 3.x吗?在Python 3.x中,binascii.hexlify返回bytes对象。 - falsetru
4
@GrijeshChauhan,请查看内置类型 - Bytes - falsetru
1
对于其他读者:使用 b'8535eaf1'.decode('ascii') 将 b'8535eaf1' 转换为 '8535eaf1'。 - mkingston
2
@mkingston,你可以省略编码:b'8535eaf1'.decode() - falsetru
显示剩余4条评论

69

考虑 Python 3.5 及以上版本中 bytes 类型的 hex() 方法

>>> array_alpha = [ 133, 53, 234, 241 ]
>>> print(bytes(array_alpha).hex())
8535eaf1

编辑:与hexlify相比速度更快(修改自@falsetru的基准测试结果)

from timeit import timeit
N = 10000
print("bytearray + hexlify ->", timeit(
    'binascii.hexlify(data).decode("ascii")',
    setup='import binascii; data = bytearray(range(255))',
    number=N,
))
print("byte + hex          ->", timeit(
    'data.hex()',
    setup='data = bytes(range(255))',
    number=N,
))

结果:

bytearray + hexlify -> 0.011218150997592602
byte + hex          -> 0.005952142993919551

1
请注意,bytearray + hexlify 返回的数据为字节(b'34567890aed'),而byte + hex 返回的是字符串(34567890aed)。 - Antonin GAVREL

14
hex_string = "".join("%02x" % b for b in array_alpha)

4
感谢您提供的答案,它也适用于旧版本的Python(我这里被迫使用2.5.1)。 - Baldrickk
@Baldrickk 是的,离题的答案有多少都很惊人。这个问题被标记为 python2.7 - personal_cloud

6
如果您有一个numpy数组,可以执行以下操作:
>>> import numpy as np
>>> a = np.array([133, 53, 234, 241])
>>> a.astype(np.uint8).data.hex()
'8535eaf1'

3
这需要导入一个外部库,而且没有解决 OP 正在使用字节的事实。这不是最健壮的解决方案。 - Mad Physicist
1
我仍然认为回答OP的问题是一个好主意,而不是发布一篇可能对某些人有用的通用帖子。很不可能有人在谷歌上搜索如何处理numpy数组,并找到标题为“Byte Array to Hex String”的问题。 - Mad Physicist
1
这只是一种更糟糕的拼写方式,等同于 bytearray([133, 53, 234, 241]).hex() - Eric
@Eric 除非你是从一个numpy“byte数组”开始。 - ostrokach
1
我觉得我误解了你的回答。你的意思是“如果你有一个numpy数组,你可以这样做”,而不是“你可以使用numpy作为工具来完成这个”。 - Eric
显示剩余2条评论

4
或者,如果您喜欢函数式编程:
>>> a = [133, 53, 234, 241]
>>> "".join(map(lambda b: format(b, "02x"), a))
8535eaf1
>>>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接