Program to convert a BCD to Hexa-Decimal Number

Given a BCD (Binary Coded Decimal) Number, the task is to convert this Binary Coded Decimal number to its equivalent Hexa-Decimal Number.

Examples:

Input: 100000101111
Output: 82F
Explanation:
Dividing the number into chunks of 4, it becomes 1000 0010 1111.
Here, 1000 is equivalent to 8,
0010 is equivalent to 2 and
1111 is equivalent to F
Therefore, the number becomes 82F



Input:10101101
Output:AD
Explanation:
Dividing the number into chunks of 4, it becomes 1010 1101.
Here, 1010 is equivalent to A and
1101 is equivalent to D.
Therefore, the number becomes AD.

Approach:

  1. Divide the given BCD number into chunks of 4, and start computing its equivalent HexaDecimal form.
  2. Store this number formed in a vector.
  3. Repeat the process for all the digits of the given BCD number.
  4. Print the numbers stored in the vector in reverse order.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ code to convert BCD to its
// HexaDecimal number(base 16).
  
// Including Header Files
#include <bits/stdc++.h>
using namespace std;
  
// Function to convert
// BCD to HexaDecimal
void bcdToHexaDecimal(string s)
{
    int len = s.length(), check = 0;
    int num = 0, sum = 0, mul = 1;
    vector<char> ans;
  
    // Iterating through
    // the bits backwards
    for (int i = len - 1; i >= 0; i--) {
        sum += (s[i] - '0') * mul;
        mul *= 2;
        check++;
  
        // Computing the HexaDecimal
        // Number formed so far
        // and storing it in a vector.
        if (check == 4 || i == 0) {
            if (sum <= 9)
                ans.push_back(sum + '0');
            else
                ans.push_back(sum + 55);
  
            // Reinitializing all
            // variables for next group.
            check = 0;
            sum = 0;
            mul = 1;
        }
    }
  
    len = ans.size();
  
    // Printing the Hexadecimal
    // number formed so far.
    for (int i = len - 1; i >= 0; i--)
        cout << ans[i];
}
  
// Driver Code
int main()
{
    string s = "100000101111";
  
    // Function Call
    bcdToHexaDecimal(s);
  
    return 0;
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to convert BCD to its
# hexadecimal number(base 16).
  
# Function to convert BCD to hexadecimal
def bcdToHexaDecimal(s):
      
    len1 = len(s)
    check = 0
    num = 0
    sum = 0
    mul = 1
    ans = []
  
    # Iterating through the bits backwards
    i = len1 - 1
      
    while(i >= 0):
        sum += (ord(s[i]) - ord('0')) * mul
        mul *= 2
        check += 1
  
        # Computing the hexadecimal number formed 
        # so far and storing it in a vector.
        if (check == 4 or i == 0):
              
            if (sum <= 9):
                ans.append(chr(sum + ord('0')))
            else:
                ans.append(chr(sum + 55));
  
            # Reinitializing all variables 
            # for next group.
            check = 0
            sum = 0
            mul = 1
          
        i -= 1
          
    len1 = len(ans)
  
    # Printing the hexadecimal
    # number formed so far.
    i = len1 - 1
      
    while(i >= 0):
        print(ans[i], end = "")
        i -= 1
  
# Driver Code
if __name__ == '__main__':
      
    s = "100000101111"
  
    # Function Call
    bcdToHexaDecimal(s)
  
# This code is contributed by Samarth

chevron_right


Output:

82F

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : ipg2016107