0

I want to convert this hexadecimal array :

[7,3,2,0,1,9,0,4]

into this one

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

where you can recognize the first 4 integers is egal to 7 in binary format (0111), and so on.

I tried to use format(x, '04b') but the result is in string format :

['0111','0011','0010','0000','0001','1001','0000','0100']

Consequently I can't use the result as binary array. How to do that ?

5
  • One way would be list(map(int,list(''.join(a)))) where a is your current result but it seems a bit ugly. Commented Oct 24, 2018 at 13:31
  • Actually, my first approach could be shortened to list(map(int,''.join(a))) Commented Oct 24, 2018 at 13:42
  • 1
    What makes [7,3,2,0,1,9,0,4] a hexadecimal array? It looks like it only contains decimal integers. Commented Oct 24, 2018 at 13:57
  • 1
    @usr2564301: I know that. I'm asking the OP why they're calling it a hexadecimal array. Commented Oct 24, 2018 at 14:09
  • @usr2564301: I believe all integers are binary in computers. What's in the OP question appears to me to be decimal representation of an "array" of them. Let's stop talking about this topic, it's boring. Commented Oct 24, 2018 at 14:32

3 Answers 3

1

This one liner will return a list of integers as you want:

hex = [7,3,2,0,1,9,0,4]
list(map(int,"".join([format(x, '04b') for x in hex])))
Sign up to request clarification or add additional context in comments.

Comments

1

You can use bitwise operations:

h = [7,3,2,0,1,9,0,4]
[i >> b & 1 for i in h for b in range(3, -1, -1)]

This returns:

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

Comments

0
arr = [7,3,2,0,1,9,0,4]

hexa = ''.join(str(e) for e in arr)

print(bin(int(hexa,16))[2:])

This takes a hexidecimal array and converts it into binary!

2 Comments

you and exchange print for x = then, use x.split() to separate it into an array
But it returns a string, and the OP wants a list of integers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.