简体   繁体   English

如何将Python中的十六进制字符串转换为字节?

[英]How to convert hexadecimal string to bytes in Python?

I have a long Hex string that represents a series of values of different types.我有一个很长的十六进制字符串,代表一系列不同类型的值。 I need to convert this Hex String into bytes or bytearray so that I can extract each value from the raw data.我需要将此十六进制字符串转换为bytesbytearray数组,以便我可以从原始数据中提取每个值。 How can I do this?我怎样才能做到这一点?

For example, the string "ab" should convert to the bytes b"\xab" or equivalent byte array.例如,字符串"ab"应转换为字节b"\xab"或等效的字节数组。 Longer example:更长的例子:

>>> # what to use in place of `convert` here?
>>> convert("8e71c61de6a2321336184f813379ec6bf4a3fb79e63cd12b")
b'\x8eq\xc6\x1d\xe6\xa22\x136\x18O\x813y\xeck\xf4\xa3\xfby\xe6<\xd1+'

Suppose your hex string is something like假设您的十六进制字符串类似于

>>> hex_string = "deadbeef"

Convert it to a bytearray (Python 3 and 2.7):将其转换为字节数组(Python 3 和 2.7):

>>> bytearray.fromhex(hex_string)
bytearray(b'\xde\xad\xbe\xef')

Convert it to a bytes object (Python 3):将其转换为字节对象(Python 3):

>>> bytes.fromhex(hex_string)
b'\xde\xad\xbe\xef'

Note that bytes is an immutable version of bytearray .请注意, bytesbytearray的不可变版本。

Convert it to a string (Python ≤ 2.7):将其转换为字符串(Python ≤ 2.7):

>>> hex_data = hex_string.decode("hex")
>>> hex_data
"\xde\xad\xbe\xef"

There is a built-in function in bytearray that does what you intend. bytearray 中有一个内置函数可以执行您想要的操作。

bytearray.fromhex("de ad be ef 00")

It returns a bytearray and it reads hex strings with or without space separator.它返回一个字节数组,并读取带或不带空格分隔符的十六进制字符串。

provided I understood correctly, you should look for binascii.unhexlify如果我理解正确,你应该寻找 binascii.unhexlify

import binascii
a='45222e'
s=binascii.unhexlify(a)
b=[ord(x) for x in s]

Assuming you have a byte string like so假设你有一个像这样的字节字符串

"\x12\x45\x00\xAB" "\x12\x45\x00\xAB"

and you know the amount of bytes and their type you can also use this approach并且您知道字节数及其类型,您也可以使用这种方法

import struct

bytes = '\x12\x45\x00\xAB'
val = struct.unpack('<BBH', bytes)

#val = (18, 69, 43776)

As I specified little endian (using the '<' char) at the start of the format string the function returned the decimal equivalent.当我在格式字符串的开头指定小端(使用'<'字符)时,该函数返回十进制等效值。

0x12 = 18 0x12 = 18

0x45 = 69 0x45 = 69

0xAB00 = 43776 0xAB00 = 43776

B is equal to one byte (8 bit) unsigned B 等于一个字节(8 位)无符号

H is equal to two bytes (16 bit) unsigned H 等于两个字节(16 位)无符号

More available characters and byte sizes can be found here更多可用的字符和字节大小可以在这里找到

The advantages are..优点是..

You can specify more than one byte and the endian of the values您可以指定多个字节和值的字节序

Disadvantages..缺点..

You really need to know the type and length of data your dealing with你真的需要知道你处理的数据的类型和长度

You can use the Codecs module in the Python Standard Library, ie您可以使用 Python 标准库中的Codecs 模块,即

import codecs

codecs.decode(hexstring, 'hex_codec')

You should be able to build a string holding the binary data using something like:您应该能够使用以下内容构建一个包含二进制数据的字符串:

data = "fef0babe"
bits = ""
for x in xrange(0, len(data), 2)
  bits += chr(int(data[x:x+2], 16))

This is probably not the fastest way (many string appends), but quite simple using only core Python.这可能不是最快的方法(许多字符串附加),但仅使用核心 Python 就非常简单。

Sometimes byte-array conversion doesn't work so in this case you can do this:有时字节数组转换不起作用,所以在这种情况下你可以这样做:

hex_string = "deadbeef"
data = ["0x" + hex_string[i:i+2] for i in range(0,len(hex_string), 2)]
def hex2bin(s):
    hex_table = ['0000', '0001', '0010', '0011',
                 '0100', '0101', '0110', '0111',
                 '1000', '1001', '1010', '1011',
                 '1100', '1101', '1110', '1111']
    bits = ''
    for i in range(len(s)):
        bits += hex_table[int(s[i], base=16)]
    return bits

A good one liner is:一个好的班轮是:

byte_list = map(ord, hex_string)

This will iterate over each char in the string and run it through the ord() function.这将遍历字符串中的每个字符并通过 ord() 函数运行它。 Only tested on python 2.6, not too sure about 3.0+.仅在 python 2.6 上测试过,不太确定 3.0+。

-Josh -乔什

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM