在python中,如何将十六进制ascii字符串转换为原始内部二进制字符串?

时间:2021-01-09 18:25:56

In python, how to convert a hex ASCII string to binary string?

在python中,如何将十六进制ASCII字符串转换为二进制字符串?

Example:

例:

01000001B8000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F202122232425262728292A2B2C2D2E2F303132333435362021222324

01000001B8000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F202122232425262728292A2B2C2D2E2F303132333435362021222324

Needs to be converted to a binary string. (0A need to be converted to 1010, not to ASCII bit 1000001 which is 65)

需要转换为二进制字符串。 (0A需要转换为1010,而不是ASCII位1000001,即65)

edit : Changed "raw binary" in question to "raw internal binary" string for better clarity.

编辑:将有问题的“原始二进制”更改为“原始内部二进制”字符串,以便更清晰。

5 个解决方案

#1


14  

import base64
data = base64.b16decode("01000001B8000102030405")

#2


9  

Is this what you're searching for?

这是你在寻找什么?

hex_string = '0A'
'{0:b}'.format(int(hex_string, 16))
# returns '1010'

or

要么

''.join('{0:04b}'.format(int(c, 16)) for c in hex_string)

#3


7  

You probably need the .decode('hex') method of strings (Python 2.x).

您可能需要.decode('hex')字符串方法(Python 2.x)。

data= ("01000001B8000102030405060708090A0B0C0D0E0F10111213141516"
    "1718191A1B1C1D1E1F202122232425262728292A2B"
    "2C2D2E2F303132333435362021222324")
data.decode('hex')

Otherwise, you can use base64.b16decode, but you might want to supply a True to the second parameter (casefold) if your input contains lowercase hex digits A to F.

否则,您可以使用base64.b16decode,但如果您的输入包含小写十六进制数字A到F,您可能希望为第二个参数(casefold)提供True。

#4


2  

I'm not quite sure what you mean by a "binary string". If you mean a string storing the binary data you can use the binascii module.

我不太确定你的意思是“二进制字符串”。如果您的意思是存储二进制数据的字符串,您可以使用binascii模块。

>>> data = "01000001B8000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F202122232425262728292A2B2C2D2E2F303132333435362021222324"
>>> import binascii
>>> binary = binascii.a2b_hex(data)
>>> binary
'\x01\x00\x00\x01\xb8\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f !"#$%&\'()*+,-./0123456 !"#$'

However, if you really want a string containing lots of "0"s and "1"s you need to go one stage further:

但是,如果你真的想要一个包含大量“0”和“1”的字符串,你需要进一步:

>>> "".join("{:08b}".format(ord(i)) for i in binary)
'0000000100000000000000000000000110111000000000000000000100000010000000110000010000000101000001100000011100001000000010010000101000001011000011000000110100001110000011110001000000010001000100100001001100010100000101010001011000010111000110000001100100011010000110110001110000011101000111100001111100100000001000010010001000100011001001000010010100100110001001110010100000101001001010100010101100101100001011010010111000101111001100000011000100110010001100110011010000110101001101100010000000100001001000100010001100100100'

#5


1  

An easy way to do what you're trying to do... convert your string to a hex decimal then use the built in bin function to convert it to binary.

一个简单的方法来做你正在尝试做的事情...将你的字符串转换为十六进制十进制,然后使用内置的bin函数将其转换为二进制。

dec_string = int(your_string, 16) #cast as int

bin_string = bin(dec_string) #convert to binary

#1


14  

import base64
data = base64.b16decode("01000001B8000102030405")

#2


9  

Is this what you're searching for?

这是你在寻找什么?

hex_string = '0A'
'{0:b}'.format(int(hex_string, 16))
# returns '1010'

or

要么

''.join('{0:04b}'.format(int(c, 16)) for c in hex_string)

#3


7  

You probably need the .decode('hex') method of strings (Python 2.x).

您可能需要.decode('hex')字符串方法(Python 2.x)。

data= ("01000001B8000102030405060708090A0B0C0D0E0F10111213141516"
    "1718191A1B1C1D1E1F202122232425262728292A2B"
    "2C2D2E2F303132333435362021222324")
data.decode('hex')

Otherwise, you can use base64.b16decode, but you might want to supply a True to the second parameter (casefold) if your input contains lowercase hex digits A to F.

否则,您可以使用base64.b16decode,但如果您的输入包含小写十六进制数字A到F,您可能希望为第二个参数(casefold)提供True。

#4


2  

I'm not quite sure what you mean by a "binary string". If you mean a string storing the binary data you can use the binascii module.

我不太确定你的意思是“二进制字符串”。如果您的意思是存储二进制数据的字符串,您可以使用binascii模块。

>>> data = "01000001B8000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F202122232425262728292A2B2C2D2E2F303132333435362021222324"
>>> import binascii
>>> binary = binascii.a2b_hex(data)
>>> binary
'\x01\x00\x00\x01\xb8\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f !"#$%&\'()*+,-./0123456 !"#$'

However, if you really want a string containing lots of "0"s and "1"s you need to go one stage further:

但是,如果你真的想要一个包含大量“0”和“1”的字符串,你需要进一步:

>>> "".join("{:08b}".format(ord(i)) for i in binary)
'0000000100000000000000000000000110111000000000000000000100000010000000110000010000000101000001100000011100001000000010010000101000001011000011000000110100001110000011110001000000010001000100100001001100010100000101010001011000010111000110000001100100011010000110110001110000011101000111100001111100100000001000010010001000100011001001000010010100100110001001110010100000101001001010100010101100101100001011010010111000101111001100000011000100110010001100110011010000110101001101100010000000100001001000100010001100100100'

#5


1  

An easy way to do what you're trying to do... convert your string to a hex decimal then use the built in bin function to convert it to binary.

一个简单的方法来做你正在尝试做的事情...将你的字符串转换为十六进制十进制,然后使用内置的bin函数将其转换为二进制。

dec_string = int(your_string, 16) #cast as int

bin_string = bin(dec_string) #convert to binary