简体   繁体   English

从十进制转换为BCD

[英]Converting from Decimal to BCD

// C++ program to convert a decimal
// number to binary number

#include <iostream>
using namespace std;

// function to convert decimal to binary
void decToBinary(int n)
{
    // array to store binary number
    int binaryNum[1000];

    // counter for binary array
    int i = 0;
    while (n > 0) {

        // storing remainder in binary array
        binaryNum[i] = n % 2;
        n = n / 2;
        i++;
    }

    // printing binary array in reverse order
    for (int j = i - 1; j >= 0; j--)
        cout << binaryNum[j];
}

// Driver program to test above function
int main()
{
    int n = 17;
    decToBinary(n);
    return 0;
}

So this is a program to convert Decimal numbers to Binary. 因此,这是一个将十进制数转换为二进制的程序。 Now I'm trying to convert Decimal Numbers to BCD. 现在,我正在尝试将十进制数转换为BCD。 I get the concept, if I have a number like 215 for example, I separate each number [2,1,5] and then convert each number into binary so it would be 0010,0001,0101. 我得到了一个概念,例如,如果我有一个像215这样的数字,我将每个数字[2,1,5]分开,然后将每个数字转换为二进制,这样它将是0010,0001,0101。 I am just confused about implementing it. 我只是对实现它感到困惑。

Can't you just replace the % 2 and / 2 with % 10 and / 10? 您不能只将%2和/ 2替换为%10和/ 10吗? The variables will be named wrong but that's the algorithmic change. 变量将被命名为错误的,但这就是算法上的改变。

You simply have to divide your integer by digits and call your function for each digit: 您只需将整数除以数字,然后为每个数字调用函数:

void decToBCD(int n) {
    // array to store digits
    int digits[10];

    // counter for digits
    int i = 0;
    while (n > 0) {
        // storing remainder in digit array
        digits[i] = n % 10;
        n = n / 10;
        i++;
    }

    // printing binary representation of digits
    for (int j = i - 1; j >= 0; j--) {
        decToBinary(digits[j]);
        cout << " ";
    }
}

我将更新从decToBinary返回字符串而不是打印到cout的内容,然后可以编写decToBCD,它使用模10来计算数字的每个数字的整数(以与使用模2并除以同样的方式) 2以获取decToBinary中的每个位),然后为每个整数位数调用decToBinary,并连接二进制数字符串以得出完整结果。

First of all, your algorithm simply displays the binary representation of some number n , instead of dividing it into single digits and returning some set of their binary representation. 首先,您的算法仅显示数字n的二进制表示形式,而不是将其划分为单个数字并返回其二进制表示形式的集合。

To make out lives easier, we will be using standard containers and standard algorithms: 为了使生活更轻松,我们将使用标准容器和标准算法:

[...] if i have a number like 215 for example, i seperate each number [2,1,5] and then covert each number into binary so it would be 0010,0001,0101 [...]例如,如果我有一个像215这样的数字,我将每个数字[2,1,5]分开,然后将每个数字隐蔽为二进制,因此它将是0010,0001,0101

Great, it means that we need some sort of a container to hold those three representations, don't we? 太好了,这意味着我们需要某种容器来容纳这三种表示形式,不是吗? My choice would be std::vector , since it is incredibly simple and efficient! 我的选择是std::vector ,因为它非常简单和有效! You can read more about it here . 您可以在此处了解更多信息。

The mentioned vector will eventually store the binary representations, but here we encounter another problem - we actually need to somehow represent them! 提到的vector最终将存储二进制表示形式,但是在这里我们遇到另一个问题-我们实际上需要以某种方式表示它们!

Fortunately enough, the standard gives us a great tool - std::bitset , which is explained here . 幸运的是,标准为我们提供了一个很好的工具- std::bitset ,这说明这里 It is primarily used to make binary operations easier, but one of its great features is that it's also extremely good at simply being a binary representation. 它主要用于使二进制操作更容易,但是它的一大特色是它也非常擅长简单地作为二进制表示形式。

The final function could look like this: 最终功能可能如下所示:

auto dec_to_bin(int n)
{
    std::vector<std::bitset<4>> repr;
    while(n > 0){
        repr.push_back(std::bitset<4>(n % 10));
        n /= 10;
    }
    std::reverse(repr.begin(), repr.end());
    return repr;
}

What is happening here? 这是怎么回事

We firstly create a vector of fixed size bitsets (of the size 4, since every decimal digit can be represented as four binary digits), then as long as our n is greater than zero (you already know why - you are using the same logic in your code), we add (using push_back ) a new bitset, that will be treated as binary representation of modulo of your number (ie the last digit). 我们首先创建一个vector固定大小位集的(大小4的,因为每一个十进制数字可以表示为四个二进制位),然后只要我们n大于零(你已经知道为什么-您使用的是同样的逻辑在您的代码中),我们添加(使用push_back )新的位集,该位集将被视为您的数字(即最后一位)的模的二进制表示形式。

Keep in mind though, that by doing this, we created the vector in the reversed order. 但是请记住,通过执行此操作,我们以相反的顺序创建了vector The last two things we have to do is simply reverse and return it! 我们要做的最后两件事就是简单地reverse并返回它!

Finally, we can use our function in main as such: 最后,我们可以像这样在main使用我们的函数:

int main()
{
    for(auto b : dec_to_bin(215)){
        std::cout << b << ' ';
    }
}

This will print 0010 0001 0101 , which was your desired output for the number 215 这将打印0010 0001 0101 ,这是您希望输出的数字215

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM