GeeksforGeeks App
Open App
Browser
Continue

# 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: 110000

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++

 `// C++ code to convert BCD to its``// equivalent Binary` `#include ``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;``}`

## Java

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

## Python3

 `# Python3 code to convert``# BCD to its equivalent Binary` `# Function to convert``# BCD to Decimal``def` `bcdToBinary(s):` `    ``l ``=` `len``(s)``    ``num ``=` `0``;``    ``mul ``=` `1``;``    ``sum` `=` `0``;``     ` `    ``# If the length of given``    ``# BCD is not divisible by 4   ``    ``for` `i ``in` `range``(l ``%` `4` `-` `1``,``                   ``-``1``, ``-``1``):   ``        ``sum` `+``=` `(``ord``(s[i]) ``-``                ``ord``(``'0'``)) ``*` `mul;``        ``mul ``*``=` `2``;``    ` `    ``num ``=` `sum``;``    ``sum` `=` `0``;``    ``mul ``=` `pow``(``2``, ``3``);``    ``ctr ``=` `0``;``    ` `    ``for` `i ``in` `range``(l ``%` `4``, l):   ``        ``ctr ``+``=` `1``;``        ``sum` `+``=` `(``ord``(s[i]) ``-``                ``ord``(``'0'``)) ``*` `mul;``        ``mul ``/``/``=` `2``;``        ``if` `(ctr ``=``=` `4``):``            ``num ``=` `num ``*` `10` `+` `sum``;``            ``sum` `=` `0``;``            ``mul ``=` `pow``(``2``, ``3``);``            ``ctr ``=` `0``;       `` ` `    ``# Convert decimal to binary``    ``ans ``=` `"";``    ``while` `(num > ``0``):``        ``ans ``+``=` `(``chr``((num ``%` `2``) ``+``                ``ord``(``'0'``)));``        ``num ``/``/``=` `2``;``    ``ans ``=` `ans[:: ``-``1``]``    ` `    ``return` `ans;` `# Driver code``if` `__name__ ``=``=` `"__main__"``:``    ` `    ``s ``=` `"1001000"``;`` ` `    ``# Function Call``    ``print``(bcdToBinary(s));` `# This code is contributed by rutvik_56`

## C#

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

## Javascript

 ``

Output:

`110000`

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

My Personal Notes arrow_drop_up