字节数组到十六进制字符串 [英] Byte Array to Hex String

查看:50
本文介绍了字节数组到十六进制字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将数据存储在字节数组中.如何将此数据转换为十六进制字符串?

我的字节数组示例:

array_alpha = [ 133, 53, 234, 241 ]

解决方案

使用 <代码>str.format:

<预><代码>>>>array_alpha = [ 133, 53, 234, 241 ]>>>打印 ''.join('{:02x}'.format(x) for x in array_alpha)8535eaf1

或使用format<预><代码>>>>打印 ''.join(format(x, '02x') for x in array_alpha)8535eaf1

<块引用>

注意: 在格式语句中,02 意味着它会在必要时填充最多 2 个前导 0 .这很重要,因为 [0x1, 0x1, 0x1] 即 (0x010101) 将被格式化为 "111" 而不是 "010101" >

或使用 bytearraybinascii.hexlify:

<预><代码>>>>导入二进制文件>>>binascii.hexlify(bytearray(array_alpha))'8535eaf1'

<小时>

以下是 Python 3.6.1 中上述方法的基准:

from timeit import timeit导入二进制文件数字 = 10000def using_str_format() ->字符串:return "".join("{:02x}".format(x) for x in test_obj)def using_format() ->字符串:return "".join(format(x, "02x") for x in test_obj)def using_hexlify() ->字符串:返回 binascii.hexlify(bytearray(test_obj)).decode('ascii')def do_test():print("用{}-byte {}测试:".format(len(test_obj), test_obj.__class__.__name__))如果 using_str_format() != using_format() != using_hexlify():raise RuntimeError("结果不一样")打印(使用 str.format -> " + str(timeit(using_str_format, number=number)))打印(使用格式 -> " + str(时间(使用格式,数字=数字)))打印(使用 binascii.hexlify ->" + str(timeit(using_hexlify, number=number)))test_obj = bytes([i for i in range(255)])做_测试()test_obj = bytearray([i for i in range(255)])做_测试()

结果:

使用 255 字节字节进行测试:使用 str.format ->1.459474583090427使用格式 ->1.5809937679100738使用 binascii.hexlify ->0.014521426401399307使用 255 字节字节数组进行测试:使用 str.format ->1.443447684109402使用格式 ->1.5608712609513171使用 binascii.hexlify ->0.014114164661833684

使用 format 的方法确实提供了额外的格式化选项,例如用空格 " ".join、逗号 "、".join 分隔数字, 大写打印 "{:02X}".format(x)/format(x, "02X") 等,但以牺牲性能为代价影响.

I have data stored in a byte array. How can I convert this data into a hex string?

Example of my byte array:

array_alpha = [ 133, 53, 234, 241 ]

解决方案

Using str.format:

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

or using format

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

Note: In the format statements, the 02 means it will pad with up to 2 leading 0s if necessary. This is important since [0x1, 0x1, 0x1] i.e. (0x010101) would be formatted to "111" instead of "010101"

or using bytearray with binascii.hexlify:

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


Here is a benchmark of above methods in 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()

Result:

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

Methods using format do provide additional formatting options, as example separating numbers with spaces " ".join, commas ", ".join, upper-case printing "{:02X}".format(x)/format(x, "02X"), etc., but at a cost of great performance impact.

这篇关于字节数组到十六进制字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆