Python Program to Convert Binary to Hexadecimal
Last Updated :
16 Feb, 2023
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
Method 1: User-defined Code To Convert Binary to Hexadecimal
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:
Python3
def binToHexa(n):
bnum = int (n)
temp = 0
mul = 1
count = 1
hexaDeciNum = [ '0' ] * 100
i = 0
while bnum ! = 0 :
rem = bnum % 10
temp = temp + (rem * mul)
if count % 4 = = 0 :
if temp < 10 :
hexaDeciNum[i] = chr (temp + 48 )
else :
hexaDeciNum[i] = chr (temp + 55 )
mul = 1
temp = 0
count = 1
i = i + 1
else :
mul = mul * 2
count = count + 1
bnum = int (bnum / 10 )
if count ! = 1 :
hexaDeciNum[i] = chr (temp + 48 )
if count = = 1 :
i = i - 1
print ( "\n Hexadecimal equivalent of {}: " . format (n), end = "")
while i > = 0 :
print (end = hexaDeciNum[i])
i = i - 1
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:
Python3
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)
def decToHexa(n):
hexaDeciNum = [ '0' ] * 100
i = 0
while (n ! = 0 ):
temp = 0
temp = n % 16
if (temp < 10 ):
hexaDeciNum[i] = chr (temp + 48 )
i = i + 1
else :
hexaDeciNum[i] = chr (temp + 55 )
i = i + 1
n = int (n / 16 )
j = i - 1
while (j > = 0 ):
print ((hexaDeciNum[j]), end = "")
j = j - 1
print ()
def binToHexa(n):
decimal = binaryToDecimal(n)
print ( "Hexadecimal equivalent of {}: " . format (n))
decToHexa(decimal)
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().
Python3
def binToHexa(n):
num = int (n, 2 )
hex_num = hex (num)
return (hex_num)
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().
Python3
def binToHexa(n):
num = int (n, 2 )
hex_num = format (num, 'x' )
return (hex_num)
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)
Share your thoughts in the comments
Please Login to comment...