# 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 becomes1000 0010 1111.

Here,1000is equivalent to8,

0010is equivalent to2and

1111is equivalent toF

Therefore, the number becomes82F

Input:10101101

Output:AD

Explanation:

Dividing the number into chunks of 4, it becomes1010 1101.

Here,1010is equivalent toAand

1101is equivalent toD.

Therefore, the number becomesAD.

**Approach:**

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

Below is the implementation of the above approach:

## C++

`// 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*

*filter_none*

## Python3

`# 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*

*filter_none*

**Output:**

82F

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: **DSA Self Paced**. Become industry ready at a student-friendly price.

## Recommended Posts:

- Program to Convert Hexadecimal Number to Binary
- Program to Convert Octal to Hexadecimal
- Program to Convert Hexadecimal to Octal
- Convert a binary number to hexadecimal number
- Program for Hexadecimal to Decimal
- Program to Convert Octal Number to Binary Number
- Program for decimal to hexadecimal conversion
- Program to convert Number in characters
- Program to convert a given number to words
- Program to convert a given number to words | Set 2
- Program to Convert BCD number into Decimal number
- Program to convert Hexa-Decimal Number to its equivalent BCD
- Python program to convert decimal to binary number
- Convert Hexadecimal value String to ASCII value String
- Check if a HexaDecimal number is Even or Odd
- Count Hexadecimal Number
- Reverse bytes of a Hexadecimal Number
- Largest Even and Odd N-digit numbers in Hexadecimal Number System
- Program to convert Degree to Radian
- Program to Convert Radian to Degree

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.