Convert the given BCD to its equivalent Binary form

Given a BCD (Binary Coded Decimal), the task is to convert this to its equivalent Binary number
Examples: 
 

Input: 1001000 
Output: 110000 
Explanation: 
Integer value of the given BCD is 48(0100 -> 4, 1000 -> 8). 
(48)10 = (110000)2
Input: 1001001 
Output: 110001 
 

 

Approach: In order to solve this problem we need to split the given BCD number into binary chunks of length 4 and convert them to integer one by one to generate the final integer representation of the given BCD. Once generated, convert the integer to its binary form.
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
// equivalent Binary
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to convert BCD to Decimal
string bcdToBinary(string s)
{
    int l = s.length();
    int num = 0;
    int mul = 1;
    int sum = 0;
     
     // If the length of given BCD is not
    // divisible by 4
    for (int i = l % 4 - 1; i >= 0; i--)
    {
        sum += (s[i] - '0') * mul;
        mul *= 2;
    }
    num = sum;
    sum = 0;
    mul = pow(2, 3);
    int ctr = 0;
    for (int i = l % 4; i < l; i++)
    {
        ctr++;
        sum += (s[i] - '0') * mul;
        mul /= 2;
        if (ctr == 4) {
            num = num * 10 + sum;
            sum = 0;
            mul = pow(2, 3);
            ctr = 0;
        }
    }
 
    // Convert decimal to binary
    string ans = "";
    while (num > 0) {
        ans += (char)(num % 2 + '0');
        num /= 2;
    }
    reverse(ans.begin(), ans.end());
    return ans;
}
 
// Driver Code
int main()
{
    string s = "1001000";
 
    // Function Call
    cout << bcdToBinary(s);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to convert BCD to its
// equivalent Binary
import java.io.*;
import java.util.*;
 
class GFG{
     
// Function to convert BCD to Decimal
static String bcdToBinary(String s)
{
    int l = s.length();
    int num = 0;
    int mul = 1;
    int sum = 0;
     
    // If the length of given BCD is not
    // divisible by 4
    for(int i = l % 4 - 1; i >= 0; i--)
    {
        sum += (s.charAt(i) - '0') * mul;
        mul *= 2;
    }
 
    num = sum;
    sum = 0;
    mul = (int)Math.pow(2, 3);
    int ctr = 0;
     
    for(int i = l % 4; i < l; i++)
    {
        ctr++;
        sum += (s.charAt(i) - '0') * mul;
        mul /= 2;
         
        if (ctr == 4)
        {
            num = num * 10 + sum;
            sum = 0;
            mul = (int)Math.pow(2, 3);
            ctr = 0;
        }
    }
     
    // Convert decimal to binary
    String ans = "";
    while (num > 0)
    {
        ans += (char)(num % 2 + '0');
        num /= 2;
    }
 
    StringBuilder ans1 = new StringBuilder();
 
    // Append a string into StringBuilder input1
    ans1.append(ans);
 
    // Reverse StringBuilder input1
    ans = ans1.reverse().toString();
 
    return ans;
}
 
// Driver code
public static void main(String[] args)
{
    String s = "1001000";
     
    // Function call
    System.out.println(bcdToBinary(s));
}
}
 
// This code is contributed by coder001

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# code to convert BCD to its
// equivalent Binary
using System;
using System.Text;
public class GFG{
      
// Function to convert BCD to Decimal
static String bcdToBinary(String s)
{
    int l = s.Length;
    int num = 0;
    int mul = 1;
    int sum = 0;
      
    // If the length of given BCD is not
    // divisible by 4
    for(int i = l % 4 - 1; i >= 0; i--)
    {
        sum += (s[i] - '0') * mul;
        mul *= 2;
    }
    num = sum;
    sum = 0;
    mul = (int)Math.Pow(2, 3);
    int ctr = 0;
    for(int i = l % 4; i < l; i++)
    {
        ctr++;
        sum += (s[i] - '0') * mul;
        mul /= 2;
        if (ctr == 4)
        {
            num = num * 10 + sum;
            sum = 0;
            mul = (int)Math.Pow(2, 3);
            ctr = 0;
        }
    }
      
    // Convert decimal to binary
    String ans = "";
    while (num > 0)
    {
        ans += (char)(num % 2 + '0');
        num /= 2;
    }
    StringBuilder ans1 = new StringBuilder();
  
    // Append a string into StringBuilder input1
    ans1.Append(ans);
  
    // Reverse StringBuilder input1
    ans = reverse(ans1.ToString());
    return ans;
}
static String reverse(String input)
{
        char[] a = input.ToCharArray();
        int l, r = a.Length - 1;
        for (l = 0; l < r; l++, r--)
        {
            char temp = a[l];
            a[l] = a[r];
            a[r] = temp;
        }
        return String.Join("",a);
    }
   
// Driver code
public static void Main(String[] args)
{
    String s = "1001000";
      
    // Function call
    Console.WriteLine(bcdToBinary(s));
}
}
 
// This code is contributed by shikhasingrajput

chevron_right


Output: 

110000


 

Time Complexity: O(N) where N denotes the length of the BCD string provided 
Auxiliary Space: O(1)
 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




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 : coder001, shikhasingrajput