给定一个BCD(二进制编码的十进制) ,任务是将其转换为等效的二进制数。
例子:
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
方法:为了解决此问题,我们需要将给定的BCD编号拆分为长度为4的二进制块,并将它们一一转换为整数,以生成给定BCD的最终整数表示形式。生成后,将整数转换为其二进制形式。
下面是上述方法的实现。
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
输出:
110000
时间复杂度: O(N) ,其中N表示提供的BCD字符串的长度
辅助空间: O(1)