Reputation: 303
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. 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
.
I want to separate this result of elements 0 0 0 0 0 0 0 1
and put in dynamicArrayBin=[]
.
After a while when I call dynamicArrayBin=[0]
,it should show 0
.
Is there any way to do it?
Upvotes: 1
Views: 873
Reputation: 1121744
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).
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.
So:
print(binary_representation[0])
works and prints 0
or 1
.
If you must have a list, you can do so with 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
.
Upvotes: 3
Reputation: 416
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']]
Upvotes: 0