12

I have a hex value in a string like

h = '00112233aabbccddee'

I know I can convert this to binary with:

h = bin(int(h, 16))[2:]

However, this loses the leading 0's. Is there anyway to do this conversion without losing the 0's? Or is the best way to do this just to count the number of leading 0's before the conversion then add it in afterwards.

root
  • 645
  • 2
  • 6
  • 6
  • 1
    Is there any particular reason you need the `0`'s? – Wayne Werner Jul 15 '10 at 17:52
  • 1
    I'm using the value h later as a key to a dictionary. The key however has all of the leading 0's. – root Jul 15 '10 at 17:57
  • Is there a compelling reason you're using a binary string representation of the value rather than an integer for the dictionary key? – Russell Borogove Jul 15 '10 at 19:53
  • @WayneWerner: I was looking for this so I can easily compare 50 binary numbers and look for bits that aren't changing. `[bin(x).rjust(20) for x in a]` is good enough. – endolith Mar 07 '12 at 17:35
  • related: [Convert binary to ASCII and vice versa](http://stackoverflow.com/q/7396849/4279) – jfs Mar 27 '16 at 15:29

7 Answers7

25

I don't think there is a way to keep those leading zeros by default.

Each hex digit translates to 4 binary digits, so the length of the new string should be exactly 4 times the size of the original.

h_size = len(h) * 4

Then, you can use .zfill to fill in zeros to the size you want:

h = ( bin(int(h, 16))[2:] ).zfill(h_size)
Donald Miner
  • 38,889
  • 8
  • 95
  • 118
  • I think this gave me 3 extra 0's for some reason – root Jul 15 '10 at 18:00
  • Ah nevermind it works perfectly. I wasn't counting the 0's before the first non zero hex number. Thanks. – root Jul 15 '10 at 18:04
  • 1
    @root: unrelated: don't reuse `h` name for different things e.g., use `bits` name for the result: `bits = bin(int(h, 16))[2:].zfill(len(h) * 4)` – jfs Mar 27 '16 at 15:32
14

This is actually quite easy in Python, since it doesn't have any limit on the size of integers. Simply prepend a '1' to the hex string, and strip the corresponding '1' from the output.

>>> h = '00112233aabbccddee'
>>> bin(int(h, 16))[2:]  # old way
'1000100100010001100111010101010111011110011001101110111101110'
>>> bin(int('1'+h, 16))[3:]  # new way
'000000000001000100100010001100111010101010111011110011001101110111101110'
Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
2

Basically the same but padding to 4 bindigits each hexdigit

''.join(bin(int(c, 16))[2:].zfill(4) for c in h)
Robert Jacobs
  • 3,266
  • 1
  • 20
  • 30
Nas Banov
  • 28,347
  • 6
  • 48
  • 67
0

A newbie to python such as I would proceed like so

datastring = 'HexInFormOfString'  

Padding to accommodate preceding zeros if any, when python converts string to Hex.

datastrPadded = 'ffff' + datastring 

Convert padded value to binary.

databin = bin(int(datastrPadded,16))

Remove 2bits ('0b') that python adds to denote binary + 16 padded bits .

databinCrop = databin[18:]
AdKash
  • 1
  • 2
0

This converts a hex string into a binary string. Since you want the length to be dependent on the original, this may be what you want.

data = ""
while len(h) > 0:
    data = data + chr(int(h[0:2], 16))
    h = h[2:]
print h
Robert Jacobs
  • 3,266
  • 1
  • 20
  • 30
0

I needed integer as input and pure hex/bin strings out with the prefixs '0b' and '0x' so my general solution is this:

def pure_bin(data, no_of_bits=NO_OF_BITS):
    data = data + 2**(no_of_bits)
    return bin(data)[3:]

def pure_hex(data, no_of_bits=NO_OF_BITS):
    if (no_of_bits%4) != 0:
        no_of_bits = 4*int(no_of_bits / 4) + 4
    data = data + 2**(no_of_bits)
    return hex(data)[3:]
Peter
  • 1
-1
hexa = '91278c4bfb3cbb95ffddc668d995bfe0'
binary = bin(int(hexa, 16))[2:]
print binary
hexa_dec = hex(int(binary, 2))[2:]
print hexa_dec
Haresh Shyara
  • 1,826
  • 10
  • 13