The problem is to convert the given binary number to its equivalent hexadecimal number.
The input could be very large and may not fit even into unsigned long long int.
Input : 110001110 Output : 18E Input : 1111001010010100001.010110110011011 Output : 794A1.5B36
Memorize the value of a 4-digit binary number in hexadecimal from this if we don’t know the relation.
A simple approach is to first convert the binary number to decimal number and then convert decimal to hexadecimal.
An efficient approach for large numbers is to consider the binary input as a string of characters and then follow the steps:
- Get length of substring to the left and right of the decimal point(‘.’) as left_len and right_len.
- If left_len is not a multiple of 4 add min number of 0’s in the beginning to make length of left substring a multiple of 4.
- If right_len is not a multiple of 4 add min number of 0’s in the end to make length of right substring a multiple of 4.
- Now, from the left extract one by one substrings of length 4 and add its corresponding hex code to the result.
- If in between a decimal(‘.’) is ecountered then add it to the result.
Hexadecimal number = 794A1.5B36
Time Complexity: O(n), where n is the length of string.
This article is contributed by Ayush Jauhari. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Program to Convert Hexadecimal Number to Binary
- Program to Convert Octal Number to Binary Number
- Convert a binary number to octal
- Convert decimal fraction to binary number
- Program to Convert Hexadecimal to Octal
- Convert a number m to n using minimum number of given operations
- Convert Hexadecimal value String to ASCII value String
- Check if the binary representation of a number has equal number of 0s and 1s in blocks
- Convert to number with digits as 3 and 8 only
- Program to convert a given number to words | Set 2
- Program to convert a given number to words
- Minimum prime number operations to convert A to B
- Convert a number into negative base representation
- Convert a number of length N such that it contains any one digit at least 'K' times
- Number of steps to convert to prime factors