Python Program to Convert Binary to Hexadecimal
Last Updated :
02 Aug, 2024
Given a binary number, the task is to write a Python program to convert the given binary number into an equivalent hexadecimal number. i.e convert the number with base value 2 to base value 16. In hexadecimal representation we 16 values to represent a number. Numbers 0-9 are expressed by digits 0-9 and 10-15 are represented by characters from A – F.
Example:
Input: 1111
Output: F
Input: 110101
Output: 35
Input: 100001111
Output: 10F
Step 1: Input binary number.
Input: 111101111011
Step 2: Divide your binary number into groups of four, starting from right.
111101111011 = (1111)(0111)(1011)
Step 3: Convert one 4-digit group of a binary number to one hexadecimal digit.
(1111)(0111)(1011) = F7B
Below is the Python implementation of the above approach:
Python
# Python code to convert binary number
# into hexadecimal number
# function to convert
# binary to hexadecimal
def binToHexa(n):
bnum = int(n)
temp = 0
mul = 1
# counter to check group of 4
count = 1
# char array to store hexadecimal number
hexaDeciNum = ['0'] * 100
# counter for hexadecimal number array
i = 0
while bnum != 0:
rem = bnum % 10
temp = temp + (rem*mul)
# check if group of 4 completed
if count % 4 == 0:
# check if temp < 10
if temp < 10:
hexaDeciNum[i] = chr(temp+48)
else:
hexaDeciNum[i] = chr(temp+55)
mul = 1
temp = 0
count = 1
i = i+1
# group of 4 is not completed
else:
mul = mul*2
count = count+1
bnum = int(bnum/10)
# check if at end the group of 4 is not
# completed
if count != 1:
hexaDeciNum[i] = chr(temp+48)
# check at end the group of 4 is completed
if count == 1:
i = i-1
# printing hexadecimal number
# array in reverse order
print("\n Hexadecimal equivalent of {}: ".format(n), end="")
while i >= 0:
print(end=hexaDeciNum[i])
i = i-1
# Driver code
if __name__ == '__main__':
binToHexa('1111')
binToHexa('110101')
binToHexa('100001111')
binToHexa('111101111011')
Output:
Hexadecimal equivalent of 1111: F
Hexadecimal equivalent of 110101: 35
Hexadecimal equivalent of 100001111: 10F
Hexadecimal equivalent of 111101111011: F7B
Method 2: First Convert Binary to Decimal and then Decimal to Hexadecimal
Step 1: Input binary number.
Input: 111101111011 = (111101111011)2
Step 2: Convert binary number to decimal number.
(111101111011)2 = (3963)10
Step 3: Convert the above decimal number to a hexadecimal number.
(3963)10 = (F7B)16
Below is the Python implementation of the above approach:
Python
# Python program to convert binary number
# into hexadecimal number
# Function calculates the decimal equivalent
# to given binary number
def binaryToDecimal(binary):
binary1 = int(binary)
decimal, i, n = 0, 0, 0
while(binary1 != 0):
dec = binary1 % 10
decimal = decimal + dec * pow(2, i)
binary1 = binary1//10
i += 1
return(decimal)
# function to convert
# decimal to hexadecimal
def decToHexa(n):
# char array to store
# hexadecimal number
hexaDeciNum = ['0'] * 100
# counter for hexadecimal
# number array
i = 0
while(n != 0):
# temporary variable
# to store remainder
temp = 0
# storing remainder
# in temp variable.
temp = n % 16
# check if temp < 10
if(temp < 10):
hexaDeciNum[i] = chr(temp + 48)
i = i + 1
else:
hexaDeciNum[i] = chr(temp + 55)
i = i + 1
n = int(n / 16)
# printing hexadecimal number
# array in reverse order
j = i - 1
while(j >= 0):
print((hexaDeciNum[j]), end="")
j = j - 1
print()
# function to convert binary to
# hexadecimal
def binToHexa(n):
decimal = binaryToDecimal(n)
print("Hexadecimal equivalent of {}: ".format(n))
decToHexa(decimal)
# Driver code
if __name__ == '__main__':
binToHexa('1111')
binToHexa('110101')
binToHexa('100001111')
binToHexa('111101111011')
Output:
Hexadecimal equivalent of 1111:
F
Hexadecimal equivalent of 110101:
35
Hexadecimal equivalent of 100001111:
10F
Hexadecimal equivalent of 111101111011:
F7B
Method 3: Using Predefined Functions
Example 1: Using int() and hex()
We use int() and hex() to convert a binary number to its equivalent hexadecimal number. Below is the Python implementation using int() and hex().
Python
# Python code to convert from Binary
# to Hexadecimal using int() and hex()
def binToHexa(n):
# convert binary to int
num = int(n, 2)
# convert int to hexadecimal
hex_num = hex(num)
return(hex_num)
# Driver code
if __name__ == '__main__':
print(binToHexa('1111'))
print(binToHexa('110101'))
print(binToHexa('100001111'))
print(binToHexa('111101111011'))
Output:
0xf
0x35
0x10f
0xf7b
Example 2: Using int() and format()
We use int() and format() to convert a binary number to its equivalent hexadecimal number. Below is the Python implementation using int() and format().
Python
# Python code to convert from Binary
# to hexadecimal using format()
def binToHexa(n):
# convert binary to int
num = int(n, 2)
# convert int to hexadecimal
hex_num = format(num, 'x')
return(hex_num)
# Driver code
if __name__ == '__main__':
print(binToHexa('1111'))
print(binToHexa('110101'))
print(binToHexa('100001111'))
print(binToHexa('111101111011'))
Output:
f
35
10f
f7b
Time Complexity: O(logn)
Space Complexity: O(n)
Similar Reads
Python Program to Convert Decimal to Hexadecimal
In this article, we will learn how to convert a decimal value(base 10) to a hexadecimal value (base 16) in Python. Method 1: Using hex() function hex() function is one of the built-in functions in Python3, which is used to convert an integer number into its corresponding hexadecimal form. Syntax :
3 min read
Python program to convert binary to ASCII
In this article, we are going to see the conversion of Binary to ASCII in the Python programming language. There are multiple approaches by which this conversion can be performed that are illustrated below: Method 1: By using binascii module Binascii helps convert between binary and various ASCII-en
3 min read
Python program to convert ASCII to Binary
We are having a string s="Hello" we need to convert the string to its ASCII then convert the ASCII to Binary representation so that the output becomes : 01001000 01100101 01101100 01101100 0110111. To convert an ASCII string to binary we use ord() to get the ASCII value of each character and format(
2 min read
Program to convert IP address to hexadecimal
Given an IP Address and task is to change the IP address equivalent to the hexadecimal value. Examples: Input : 127.0.0.1Output : 0x7f000001Input : 172.31.0.2Output : 0xac1f0002Explanation: Using the Library function to convert the IP address into the hexadecimal value we use the " arpa/inet.h " hea
4 min read
Python program to convert hex string to decimal
Converting a hexadecimal string to a decimal number is a common task in programming, especially when working with binary data or low-level computations. Hexadecimal values offer a concise way to represent large numbers. Using the int() FunctionUsing the int() function is the most common and straight
2 min read
Convert Bytearray to Hexadecimal String - Python
We are given a byte array a=bytearray([15, 255, 100, 56]) we need to convert it into a hexadecimal string that looks like: 0fff6438. Python provides multiple ways to do this: Using the .hex() method (built-in)Using binascii.hexlify()Using format() with join()Letâs explore each method with examples.
2 min read
Python Program to add two hexadecimal numbers
Given two hexadecimal numbers, write a Python program to compute their sum. Examples: Input: a = "01B", b = "378" Output: 393 Explanation: B (11 in decimal) + 8 = 19 (13 in hex), hence addition bit = 3, carry = 1 1 + 7 + 1 (carry) = 9, hence addition bit = 9, carry = 0 0 + 3 + 0 (carry) = 3, hence a
6 min read
Python | Decimal to binary list conversion
The conversion of a binary list to a decimal number has been dealt in a previous article. This article aims at presenting certain shorthand to do the opposite, i.e binary to decimal conversion. Let's discuss certain ways in which this can be done. Method #1 : Using list comprehension + format() In t
6 min read
Python - Ways to convert hex into binary
We are given a hexadecimal number we need to convert it to binary. For example a = "1A3" we need to convert it to binary so that resultant output should be 110100011. Using bin() and int()We can convert hexadecimal to binary by first converting it to an integer using int() and then using the bin() f
1 min read
How to Convert Binary Data to Float in Python?
We are given binary data and we need to convert these binary data into float using Python and print the result. In this article, we will see how to convert binary data to float in Python. Example: Input: b'\x40\x49\x0f\xdb' <class 'bytes'>Output: 3.1415927410125732 <class 'float'>Explana
2 min read