📜  将十六进制十进制数转换为等效的BCD的程序

📅  最后修改于: 2021-05-25 09:41:04             🧑  作者: Mango

给定十六进制十进制数N ,任务是将数字转换为其等效的二进制编码十进制数。
例子:

方法:想法是遍历给定十六进制数字的每个数字,并找到该数字的四位二进制等效项。最后,一一打印所有转换后的数字。
下面是上述方法的实现:

C++
// C++ implementation  to convert the given
// HexaDecimal number to its equivalent BCD.
 
#include 
using namespace std;
 
// Function to convert
// HexaDecimal to its BCD
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[i] >= 47 && s[i] <= 52)
            cout << bitset<4>(s[i])
                 << " ";
        else
            cout << bitset<4>(s[i] - 55)
                 << " ";
    }
}
 
// Driver Code
int main()
{
    string s = "11F";
 
    // Function Call
    HexaDecimaltoBCD(s);
 
    return 0;
}


Java
// 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
# Python3 program to convert the given
# HexaDecimal number to its equivalent BCD
 
# Function to convert
# Haxadecimal to BCD
def 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 code
str = "11F"
 
# Function call
HexaDecimaltoBCD(str)
 
# This code is contributed by himanshu77


C#
// 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