简体   繁体   English

在8位中将十进制转换为二进制

[英]Converting decimal to Binary in 8 bits

// 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;
}

I was wondering how the coversion can be in 8 bits. 我想知道8位的覆盖范围如何。 Because if you put 2, the answer will be 10, but i want to implement it so it can become 00000010 因为如果您输入2,答案将是10,但是我想实现它,因此它可以变成00000010

Since this is marked C++, would this work for you? 由于此标记为C ++,因此对您有用吗?

#include <iostream>
#include <bitset>

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

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

    return 0;
}

If you assume that the input number fits into 8 bits, you can change printing code to: 如果您假设输入数字适合8位,则可以将打印代码更改为:

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

If you want to be able to print all numbers with multiples of 8 bits, you can change that to: 如果您希望能够以8位的倍数打印所有数字,可以将其更改为:

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


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

Also, make sure to zero-initialize the array to avoid undefined behavior. 另外,请确保将数组初始化为零,以避免未定义的行为。

int binaryNum[1000] = {};

A 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;
}

and its output: 及其输出:

00000010
0000110010000000
001100001101010000000000

You could use a lookup table: 您可以使用查找表:

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;

A lookup table is very fast. 查找表非常快。

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

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