Q:

Convert hex to binary

I have ABC123EFFF.
I want to have 001010101111000001001000111110111111111111 (i.e. binary repr. with, say, 42 digits and leading zeroes).
How?

A:

For solving the left-side trailing zero problem:

my_hexdata = "1a"

scale = 16 ## equals to hexadecimal

num_of_bits = 8

bin(int(my_hexdata, scale))[2:].zfill(num_of_bits)

It will give 00011010 instead of the trimmed version.

A:

import binascii

binary_string = binascii.unhexlify(hex_string)

Read
binascii.unhexlify
Return the binary data represented by the hexadecimal string specified as the parameter.

A:

bin(int("abc123efff", 16))[2:]

