Convert Binary to Hex in Python

Convert Binary to Hex in Python

  1. Create and Make Use of a User-Defined Function to Convert Binary to Hex in Python
  2. Use the int() and the hex() Functions to Convert Binary to Hex in Python
  3. Use the binascii Module to Convert Binary to Hex in Python
  4. Use the format() Function to Convert Binary to Hex in Python
  5. Use f-strings to Convert Binary to Hex in Python

Binary and Hexadecimal are two of the many number systems in which a numeric value can be represented in Python. This tutorial focuses on the different ways available to convert Binary to Hex in Python.

Create and Make Use of a User-Defined Function to Convert Binary to Hex in Python

We can create our user-defined function with the help of the while loop and put it in place to convert a value in Binary to Hex in Python.

The following code uses a user-defined function to convert Binary to Hex in Python.

print("Enter the Binary Number: ", end="")
bnum = int(input())

h = 0
m = 1
chk = 1
i = 0
hnum = []
while bnum!=0:
    rem = bnum%10
    h = h + (rem*m)
    if chk%4==0:
        if h<10:
            hnum.insert(i, chr(h+48))
        else:
            hnum.insert(i, chr(h+55))
        m = 1
        h = 0
        chk = 1
        i = i+1
    else:
        m = m*2
        chk = chk+1
    bnum = int(bnum/10)

if chk!=1:
    hnum.insert(i, chr(h+48))
if chk==1:
    i = i-1

print("\nEquivalent Hexadecimal Value = ", end="")
while i>=0:
    print(end=hnum[i])
    i = i-1
print()

The above code provides the following output.

Enter the Binary Number: 0101101
Equivalent Hexadecimal Value = 2D

Use the int() and the hex() Functions to Convert Binary to Hex in Python

We make use of both the int() and the hex() functions to implement this method.

Firstly, the int() method is utilized to convert the given binary number into an integer value. After this process, the hex() function converts the newly found integer value into a hexadecimal value.

The following code uses the int() and the hex() functions to convert Binary to Hex in Python.

print(hex(int('0101101', 2)))

The above code provides the following output.

0x2d

Use the binascii Module to Convert Binary to Hex in Python

Python provides a binascii module from Python 3 onwards that can be utilized to convert Binary to Hex in Python. The binascii module needs to be manually imported to the Python code in order for this method to work.

This method opens a text file, takes in the content of the file, and can return the hex value of the given data in the file using the hexlify() function.

The following code uses the binascii module to convert Binary to Hex in Python.

import binascii
bFile = open('ANYBINFILE.exe','rb')
bData = bFile.read(8)
print(binascii.hexlify(bData))

Use the format() Function to Convert Binary to Hex in Python

The format() function is one of the ways in which string formatting can be implemented in Python. The format() function is utilized to provide the formatted string inside the {} curly brackets.

The following code uses the format() function to convert Binary to Hex in Python.

print("{0:0>4X}".format(int("0101101", 2)))

The above code provides the following output.

002D

Use f-strings to Convert Binary to Hex in Python

Being introduced with Python 3.6, it is relatively the newest method in Python to implement string formatting. It can be used in the newer and latest versions of Python.

It is more efficient than its other two peers, % sign and str.format(), as it is faster and easier to understand. It also helps in implementing string formatting in Python at a faster rate than the other two methods.

The following code uses f-strings to convert Binary to Hex in Python.

bstr = '0101101'
hexstr = f'{int(bstr, 2):X}'
print(hexstr)

The above code provides the following output.

2D

Related Article - Python Binary

  • Binary Numbers Representation in Python
  • Convert a String to Binary in Python
  • Convert Binary to Int in Python
  • Related Article - Python Hex

  • Bitwise XOR of Hex Numbers in Python
  • Convert HEX to RGB in Python
  • Convert Hex to ASCII in Python
  • String to Hex in Python
  • Convert Hexadecimal to Decimal in Python