简体   繁体   English

在python 3中将十六进制转换为数组中的bin

[英]Converting hex to bin inside the array in python 3

I'm working on Visual Studio about Python Project.I have an array and user input for converting hex to dec and dec to bin. 我正在关于Python项目的Visual Studio上工作。我有一个数组和用户输入,用于将十六进制转换为dec和dec转换为bin。 I use both of them in my project (hex,dec,bin). 我在项目中都使用了它们(十六进制,十进制,二进制)。 Here basic sample of my code: 这是我的代码的基本示例:

dynamicArrayBin = [ ]
dynamicArrayHex = [ ] 
hexdec = input("Enter the hex number to binary "); 
dynamicArrayHex = [hexdec[idx:idx+2]  for idx in range(len(hexdec)) if idx%2 == 0]
binary = '{:08b}'.format(int(dynamicArrayHex[0] , 16))

So, when the user enter 01 for input, the code gives 00000001 . 因此,当用户输入01作为输入时,代码给出00000001 I want to separate this result of elements 0 0 0 0 0 0 0 1 and put in dynamicArrayBin=[] . 我想将元素0 0 0 0 0 0 0 1结果分开,并放入dynamicArrayBin=[] After a while when I call dynamicArrayBin=[0] ,it should show 0 . 过一会儿,当我调用dynamicArrayBin=[0] ,它应该显示0

Is there any way to do it? 有什么办法吗?

If you want a list of binary digits for a hexadecimal input, there is no need to split out the input into bytes first (which is what your code currently does, convert each 2 characters of hex input into an integer covering the range 0-255). 如果您想要一个十六进制输入的二进制数字列表,则无需先将输入拆分为字节(这是您的代码当前所做的,将十六进制输入的每2个字符转换为一个覆盖0-255范围的整数)。

Just convert the whole hex input to an integer, and format it to binary from there: 只需将整个十六进制输入转换为整数,然后从那里将其格式化为二进制:

integer_value = int(hexdec, 16)
byte_length = (len(hexdec) + 1) // 2  # to help format the output binary
binary_representation = format(integer_value, '0{}b'.format(byte_length * 8))

The binary_representation value is a string of '0' and '1' characters, and since strings are sequences, there is no need to turn that into a list unless you must be able to mutate individual characters. binary_representation值为字符串'0''1' ,并且由于字符串是序列,因此除非必须能够突变单个字符,否则无需将其转换为列表。

So: 所以:

print(binary_representation[0])

works and prints 0 or 1 . 工作并打印01

If you must have a list, you can do so with list(binary_representation)) . 如果必须具有列表,则可以使用list(binary_representation))

Demo: 演示:

>>> hexdec = 'deadbeef'  # 4 bytes, or 32 bits
>>> integer_value = int(hexdec, 16)
>>> byte_length = (len(hexdec) + 1) // 2  # to help format the output binary
>>> binary_representation = format(integer_value, '0{}b'.format(byte_length * 8))
>>> integer_value
3735928559
>>> byte_length
4
>>> binary_representation
'11011110101011011011111011101111'
>>> binary_representation[4]
'1'
>>> binary_representation[2]
'0'
>>> list(binary_representation)
['1', '1', '0', '1', '1', '1', '1', '0', '1', '0', '1', '0', '1', '1', '0', '1', '1', '0', '1', '1', '1', '1', '1', '0', '1', '1', '1', '0', '1', '1', '1', '1']

If all you wanted was the first bit of a hexadecimal value, then there is a faster method: 如果您想要的只是十六进制值的第一位 ,那么有一种更快的方法:

if len(hexdec) % 2:  # odd number of hex characters, needs a leading 0
    hexdec = '0'     # doesn't matter what the rest of the hex value is
print('1' if hexdec[0].lower() in '89abcdef' else '0')

because the first 4 bits of the binary representation are determined entirely by the first hexadecimal character, and the very first bit is set for hex values 8 through to F . 因为二进制表示的前4位完全由第一个十六进制字符确定,并且十六进制值8F设置了第一个位。

You can do something like below 您可以执行以下操作

hexLst = ['ABC123EFFF', 'ABC123EFEF', 'ABC123EEFF']
binLst = [bin(int(n, 16))[2:] for n in hexLst]
print(binLst)

Which will give you a output 这会给你输出

['1010101111000001001000111110111111111111', '1010101111000001001000111110111111101111', '1010101111000001001000111110111011111111']

then you can make a list from that 然后您可以从中列出一个清单

dynamicArrayBin=[list(b) for b in binLst]
print(dynamicArrayBin)

Output 产量

[['1', '0', '1', '0', '1', '0', '1', '1', '1', '1', '0', '0', '0', '0', '0', '1', '0', '0', '1', '0', '0', '0', '1', '1', '1', '1', '1', '0', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1'], ['1', '0', '1', '0', '1', '0', '1', '1', '1', '1', '0', '0', '0', '0', '0', '1', '0', '0', '1', '0', '0', '0', '1', '1', '1', '1', '1', '0', '1', '1', '1', '1', '1', '1', '1', '0', '1', '1', '1', '1'], ['1', '0', '1', '0', '1', '0', '1', '1', '1', '1', '0', '0', '0', '0', '0', '1', '0', '0', '1', '0', '0', '0', '1', '1', '1', '1', '1', '0', '1', '1', '1', '0', '1', '1', '1', '1', '1', '1', '1', '1']]

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

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