VOOZH about

URL: https://www.geeksforgeeks.org/dsa/convert-binary-number-hexadecimal-number/

⇱ Program to convert a binary number to hexadecimal number - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Program to convert a binary number to hexadecimal number

Last Updated : 2 Aug, 2024

Given a Binary Number, the task is to convert the given binary number to its equivalent hexadecimal number. The input could be very large and may not fit even into an unsigned long long int.

Examples:

Input: 110001110
Output: 18E

Input: 1111001010010100001.010110110011011
Output: 794A1.5B36 794A1D9B


Binary Number: A binary number is a number expressed in the base-2 binary numeral system, which uses only two symbols: which are 0 (zero) and 1 (one).

4HexaDecimal Number: A hexadecimal number is a positional numeral system with a radix, or base, of 16 and uses sixteen distinct symbols: which are 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, and F.

👁 Image


Convert Binary to HexaDecimal:
We all know that, 24 = 16 1
In other words, single digit in base 16 can be represented using 4 digits in base 2.  

👁 Image


To convert Binary number to HexaDecimal, the below steps are taken: 

  1. Group the given Binary Number into groups of 4 bits, each group taken individually from the left and right of the decimal point.
  2. Get length of substring to the left and right of the decimal point('.') as left_len and right_len.
  3. If left_len is not a multiple of 4, i.e., grouping into exact group of 4 bits is not possible, then add minimum number of 0's in the beginning to make length of left substring a multiple of 4.
  4. Similarly, If right_len is not a multiple of 4, then add minimum number of 0's in the end to make length of right substring a multiple of 4.
  5. Now, from the left, extract each group (substrings of length 4) one by one and add its corresponding Hexadecimal code to the result.
  6. If in between a decimal('.') is encountered then add it to the result.


Below is the implementation of the above approach: 

Output:

Hexadecimal number = 794A1.5B36


Time Complexity: O(n), where n is the length of the string.

Space Complexity: O(n) , space by map
Another approach to convert Binary Number to Hexadecimal number is to first convert the binary number to decimal number and then convert the obtained decimal number to equivalent hexadecimal number.
Practice Questions:
(1) Convert the binary number 111000 to hexa-decimal. 
(2) Convert the binary number 100100001 to hexa-decimal. 
(3) Convert the binary number 1001001111 to hexa-decimal. 
(4) What is the binary equivalent of hexa-decimal number A7C5. 
(5) What is the binary equivalent of hexa-decimal number 2A.FF.

Answers:
(1) 38
(2) 121
(3) 24F
(4) 1010011111000101
(5) 101010.11111111


Method 3:   This method is only for the number which does not contain any decimal point( . ) 

1. The zfill() function is used to add leading zeros to the binary number so that it can be divided into groups of four bits each.
2. The hex_dict dictionary is used to map each group of four bits to its hexadecimal equivalent.
3. The loop iterates over the binary number in groups of four bits each, and converts each group to its hexadecimal equivalent using the hex_dict dictionary.
4. The hexadecimal equivalent of each group is appended to the hexadecimal variable.


Output
18E

Time Complexity: O(n)
Auxiliary Space: O(1)


Comment