python – Convert hex to binary

python – Convert hex to binary

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.

import binascii

binary_string = binascii.unhexlify(hex_string)



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

python – Convert hex to binary

Convert hex to binary

I have ABC123EFFF.

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

Short answer:

The new f-strings in Python 3.6 allow you to do this using very terse syntax:

>>> f{0xABC123EFFF:0>42b}

or to break that up with the semantics:

>>> number, pad, rjust, size, kind = 0xABC123EFFF, 0, >, 42, b
>>> f{number:{pad}{rjust}{size}{kind}}

Long answer:

What you are actually saying is that you have a value in a hexadecimal representation, and you want to represent an equivalent value in binary.

The value of equivalence is an integer. But you may begin with a string, and to view in binary, you must end with a string.

Convert hex to binary, 42 digits and leading zeros?

We have several direct ways to accomplish this goal, without hacks using slices.

First, before we can do any binary manipulation at all, convert to int (I presume this is in a string format, not as a literal):

>>> integer = int(ABC123EFFF, 16)
>>> integer

alternatively we could use an integer literal as expressed in hexadecimal form:

>>> integer = 0xABC123EFFF
>>> integer

Now we need to express our integer in a binary representation.

Use the builtin function, format

Then pass to format:

>>> format(integer, 0>42b)

This uses the formatting specifications mini-language.

To break that down, heres the grammar form of it:


To make that into a specification for our needs, we just exclude the things we dont need:

>>> spec = {fill}{align}{width}{type}.format(fill=0, align=>, width=42, type=b)
>>> spec

and just pass that to format

>>> bin_representation = format(integer, spec)
>>> bin_representation
>>> print(bin_representation)

String Formatting (Templating) with str.format

We can use that in a string using str.format method:

>>> here is the binary form: {0:{spec}}.format(integer, spec=spec)
here is the binary form: 001010101111000001001000111110111111111111

Or just put the spec directly in the original string:

>>> here is the binary form: {0:0>42b}.format(integer)
here is the binary form: 001010101111000001001000111110111111111111

String Formatting with the new f-strings

Lets demonstrate the new f-strings. They use the same mini-language formatting rules:

>>> integer = 0xABC123EFFF
>>> length = 42
>>> f{integer:0>{length}b}

Now lets put this functionality into a function to encourage reusability:

def bin_format(integer, length):
    return f{integer:0>{length}b}

And now:

>>> bin_format(0xABC123EFFF, 42)


If you actually just wanted to encode the data as a string of bytes in memory or on disk, you can use the int.to_bytes method, which is only available in Python 3:

>>> help(int.to_bytes)
    int.to_bytes(length, byteorder, *, signed=False) -> bytes

And since 42 bits divided by 8 bits per byte equals 6 bytes:

>>> integer.to_bytes(6, big)

Leave a Reply

Your email address will not be published. Required fields are marked *