【问题标题】:Converting decimal to Binary in 8 bits将十进制转换为 8 位二进制
【发布时间】:2018-06-24 10:40:43
【问题描述】:
// 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 = 2;
    decToBinary(n);
    return 0;
}

我想知道如何覆盖 8 位。因为如果你输入 2,答案将是 10,但我想实现它,所以它可以变成 00000010

【问题讨论】:

  • 哎呀,抱歉,这是个意外。
  • 我想知道如何覆盖 8 位。您对 8 位或 8 位的倍数感兴趣吗?
  • 是的,因为所有十进制数都必须转换为 8 位。
  • 顺便提一下,您不需要 1000 个整数来存储转换结果。只要您的参数是一个 int,您永远不需要超过 CHAR_BIT * sizeof(int),它最终在 x86 上等于 32。

标签: c++ binary decimal


【解决方案1】:

由于这是标记为 C++,这对您有用吗?

#include <iostream>
#include <bitset>

int main(int argc, char *argv[])
{
    std::bitset<8> bits(2);

    std::cout << bits << "\n";

    return 0;
}

【讨论】:

  • 如何在程序中实现?
【解决方案2】:

如果您假设输入数字适合 8 位,您可以将打印代码更改为:

for (int j = 7; j >= 0; j--)
   cout << binaryNum[j];

如果您希望能够打印所有 8 位的倍数的数字,您可以将其更改为:

int bits = 8;
if ( i > 8 )
   bits = 8*((i + 7)/8);


for (int j = bits-1; j >= 0; j--)
   cout << binaryNum[j];

此外,请确保将数组初始化为零以避免未定义的行为。

int binaryNum[1000] = {};

working example:

// 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
    int bits = 8;
    if ( i > 8 )
       bits = 8*((i + 7)/8);

    for (int j = bits-1; j >= 0; j--)
       cout << binaryNum[j];

    cout << endl;
}

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

及其输出:

00000010
0000110010000000
001100001101010000000000

【讨论】:

  • 我非常喜欢这个答案,主要是因为你没有使用库,而且它更有意义,但你能解释一下 bits = 8*((i + 7)/8);就像我们为什么在位数大于 8 时使用该公式
  • @300SRT,如果 9 bits 需要为 16。如果 17 bits 需要为 24。如果 25 bits 必须是 32。这个公式可以解决问题。
  • 啊,好吧,我明白你在那里做了什么,现在它是有道理的。
【解决方案3】:

您可以使用查找表:

static const char conversion_table[] =
{
  "00000000", "00000001", "00000010", "00000011",
  "00000100", "00000101", "00000110", "00000111",
//...
  "11111100", "11111101", "11111110", "11111111",
};

std::string result = conversion_table[24];
std::cout << "Decimal 24 in binary: " << result << std::endl;

查找表非常快。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-03-01
    • 1970-01-01
    • 2019-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-18
    相关资源
    最近更新 更多