Input: 110001110 Output: 18E Input: 1111001010010100001.010110110011011 Output: 794A1.5B36
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).
HexaDecimal Number: A hexadecimal number is a positional numeral system with a radix, or base, of 16 and uses sixteen distinct symbols.
Convert Binary to HexaDecimal:
To convert Binary number to HexaDecimal, the below steps are taken:
- Group the given Binary Number into groups of 4 bits, each group taken individually from the left and right of the decimal point.
- 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, 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.
- 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.
- Now, from the left, extract each group (substrings of length 4) one by one and add its corresponding Hexadecimal code to the result.
- If in between a decimal(‘.’) is ecountered then add it to the result.
Below is the implementation of the above approach:
Hexadecimal number = 794A1.5B36
Time Complexity: O(n), where n is the length of the string.
Approach 2: 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.
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 firstname.lastname@example.org. 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
- Count Hexadecimal Number
- Reverse bytes of a Hexadecimal Number
- Program to Convert Hexadecimal to Octal
- Convert a number m to n using minimum number of given operations
- Count number of trailing zeros in Binary representation of a number using Bitset
- Check if the binary representation of a number has equal number of 0s and 1s in blocks
- Convert Hexadecimal value String to ASCII value String
- Convert to number with digits as 3 and 8 only
- Program to convert a given number to words
- Program to convert a given number to words | Set 2
- Number of steps to convert to prime factors
Improved By : RishabhPrabhu