Program to convert Hexa-Decimal Number to its equivalent BCD - GeeksforGeeks

14 Dec, 2020
Given a HexaDecimal Number N, the task is to convert the number to its equivalent Binary Coded Decimal.Examples:  
Input: 11F Output: 0001 0001 1111 Explanation: Binary of 1 – 0001 Binary of F – 1111 Thus, Equivalent BCD is 0001 0001 1111Input: A D Output: 1010 1101 Explanation: Binary of A – 1010 Binary of D – 1101 Thus, Equivalent BCD is 1010 1101  
 
Approach: The idea is to iterate over each digit of the given Hexa-Decimal number and find the four digit Binary Equivalent of that digit. Finally, print all the converted digits one by one.Below is the implementation of the above approach:  
C++
Java
Python3
C#
// C++ implementation  to convert the given// HexaDecimal number to its equivalent BCD. #include <bits/stdc++.h>using namespace std; // Function to convert// HexaDecimal to its BCDvoid HexaDecimaltoBCD(string s){    int len = s.length(), check = 0;    int num = 0, sum = 0, mul = 1;     // Iterating through the digits    for (int i = 0; i <= len - 1; i++) {         // check whether s[i] is a character        // or a integer between 0 to 9        // and compute its equivalent BCD        if (s[i] >= 47 && s[i] <= 52)            cout << bitset<4>(s[i])                 << " ";        else            cout << bitset<4>(s[i] - 55)                 << " ";    }} // Driver Codeint main(){    string s = "11F";     // Function Call    HexaDecimaltoBCD(s);     return 0;}
// Java implementation  to convert the given// HexaDecimal number to its equivalent BCD.public class Main{    // Function to convert    // HexaDecimal to its BCD    public static void HexaDecimaltoBCD(String s)    {        int len = s.length(), check = 0;        int num = 0, sum = 0, mul = 1;               // Iterating through the digits        for (int i = 0; i <= len - 1; i++) {                   // check whether s[i] is a character            // or a integer between 0 to 9            // and compute its equivalent BCD            if (s.charAt(i) >= 47 && s.charAt(i) <= 52)            {                String result = Integer.toBinaryString((int)s.charAt(i));                System.out.print(result.substring(result.length() - 4) + " ");            }            else            {                String result = Integer.toBinaryString((int)s.charAt(i) - 55);                System.out.print(result.substring(result.length() - 4) + " ");            }        }    }     public static void main(String[] args) {        String s = "11F";           // Function Call        HexaDecimaltoBCD(s);    }} // This code is contributed by divyesh072019
# Python3 program to convert the given# HexaDecimal number to its equivalent BCD # Function to convert# Haxadecimal to BCDdef HexaDecimaltoBCD(str):     # Iterating through the digits    for i in range(len(str)):                 # Conversion into equivalent BCD        print("{0:04b}".format(              int(str[i], 16)), end = " ") # Driver codestr = "11F" # Function callHexaDecimaltoBCD(str) # This code is contributed by himanshu77
// C# implementation  to convert the given// HexaDecimal number to its equivalent BCD.using System;class GFG {         // Function to convert    // HexaDecimal to its BCD    static void HexaDecimaltoBCD(string s)    {        int len = s.Length;                // Iterating through the digits        for (int i = 0; i <= len - 1; i++) {                    // check whether s[i] is a character            // or a integer between 0 to 9            // and compute its equivalent BCD            if (s[i] >= 47 && s[i] <= 52)            {                string result = Convert.ToString((int)s[i], 2);                Console.Write(result.Substring(result.Length - 4) + " ");            }            else            {                string result = Convert.ToString((int)s[i] - 55, 2);                Console.Write(result.Substring(result.Length - 4) + " ");            }        }    }       static void Main() {        string s = "11F";            // Function Call        HexaDecimaltoBCD(s);  }} // This code is contributed by diyeshrabadiya07
0001 0001 1111
 
himanshu77
divyesh072019
divyeshrabadiya07
base-conversion
Bit Magic
Mathematical
Mathematical
Bit Magic
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Set, Clear and Toggle a given bit of a number in C
Check whether K-th bit is set or not
Program to find parity
Write an Efficient Method to Check if a Number is Multiple of 3
Hamming code Implementation in C/C++
Program for Fibonacci numbers
Write a program to print all permutations of a given string
C++ Data Types
Set in C++ Standard Template Library (STL)
Coin Change | DP-7