Convert hex to binary

aaaaaaaaaaaaaaaaaaa picture aaaaaaaaaaaaaaaaaaa · Sep 15, 2009 · Viewed 306.6k times · Source

I have ABC123EFFF.

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

How?

Answer

Onedinkenedi picture Onedinkenedi · Feb 1, 2011

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.