十六进制字符串到python中的字节数组

问题:十六进制字符串到python中的字节数组

我有一个很长的十六进制字符串,表示一系列不同类型的值。我希望将此十六进制字符串转换为字节数组,以便可以将每个值移出并将其转换为适当的数据类型。

I have a long Hex string that represents a series of values of different types. I wish to convert this Hex String into a byte array so that I can shift each value out and convert it into its proper data type.


回答 0

假设您的十六进制字符串类似于

>>> hex_string = "deadbeef"

将其转换为字符串(Python≤2.7):

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

或从Python 2.7和Python 3.0开始:

>>> bytes.fromhex(hex_string)  # Python ≥ 3
b'\xde\xad\xbe\xef'

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

请注意,这bytes是的不变版本bytearray

Suppose your hex string is something like

>>> hex_string = "deadbeef"

Convert it to a string (Python ≤ 2.7):

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

or since Python 2.7 and Python 3.0:

>>> bytes.fromhex(hex_string)  # Python ≥ 3
b'\xde\xad\xbe\xef'

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

Note that bytes is an immutable version of bytearray.


回答 1

字节数组中有一个内置函数可以满足您的要求。

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

它返回一个字节数组,并读取带或不带空格的十六进制字符串。

There is a built-in function in bytearray that does what you intend.

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

It returns a bytearray and it reads hex strings with or without space separator.


回答 2

如果我理解正确,则应查找binascii.unhexlify

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

provided I understood correctly, you should look for binascii.unhexlify

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

回答 3

假设你有一个像这样的字节串

“ \ x12 \ x45 \ x00 \ xAB”

而且您知道字节数及其类型,也可以使用这种方法

import struct

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

#val = (18, 69, 43776)

正如我在格式字符串的开头指定小尾数(使用'<‘char)一样,该函数返回了十进制等效项。

0x12 = 18

0x45 = 69

0xAB00 = 43776

B等于一个字节(8位)无符号

H等于两个字节(16位)无符号

可在此处找到更多可用字符和字节大小

优点是..

您可以指定多个字节和值的字节序

缺点

您确实需要知道您要处理的数据的类型和长度

Assuming you have a byte string like so

“\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

0x45 = 69

0xAB00 = 43776

B is equal to one byte (8 bit) unsigned

H is equal to two bytes (16 bit) unsigned

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


回答 4

您应该可以使用以下方法构建一个包含二进制数据的字符串:

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

这可能不是最快的方法(许多字符串追加),但是仅使用核心Python相当简单。

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.


回答 5

您可以使用Python标准库中的Codecs模块,即

import codecs

codecs.decode(hexstring, 'hex_codec')

You can use the Codecs module in the Python Standard Library, i.e.

import codecs

codecs.decode(hexstring, 'hex_codec')

回答 6

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
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

回答 7

一个好的衬板是:

byte_list = map(ord, hex_string)

这将遍历字符串中的每个字符并通过ord()函数运行它。仅在python 2.6上测试过,不太确定3.0以上版本。

-乔什

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. Only tested on python 2.6, not too sure about 3.0+.

-Josh