C++ - 十进制到二进制转换

C++ - Decimal to binary converting(C++ - 十进制到二进制转换)

本文介绍了C++ - 十进制到二进制转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个简单"(花了我 30 分钟)程序,将十进制数转换为二进制数.我确信有很多更简单的方法,你能告诉我吗?代码如下:

I wrote a 'simple' (it took me 30 minutes) program that converts decimal number to binary. I am SURE that there's a lot simpler way so can you show me? Here's the code:

#include <iostream>
#include <stdlib.h>

using namespace std;
int a1, a2, remainder;
int tab = 0;
int maxtab = 0;
int table[0];
int main()
{
    system("clear");
    cout << "Enter a decimal number: ";
    cin >> a1;
    a2 = a1; //we need our number for later on so we save it in another variable

    while (a1!=0) //dividing by two until we hit 0
    {
        remainder = a1%2; //getting a remainder - decimal number(1 or 0)
        a1 = a1/2; //dividing our number by two
        maxtab++; //+1 to max elements of the table
    }

    maxtab--; //-1 to max elements of the table (when dividing finishes it adds 1 additional elemnt that we don't want and it's equal to 0)
    a1 = a2; //we must do calculations one more time so we're gatting back our original number
    table[0] = table[maxtab]; //we set the number of elements in our table to maxtab (we don't get 10's of 0's)

    while (a1!=0) //same calculations 2nd time but adding every 1 or 0 (remainder) to separate element in table
    {
        remainder = a1%2; //getting a remainder
        a1 = a1/2; //dividing by 2
        table[tab] = remainder; //adding 0 or 1 to an element
        tab++; //tab (element count) increases by 1 so next remainder is saved in another element
    }

    tab--; //same as with maxtab--
    cout << "Your binary number: ";

    while (tab>=0) //until we get to the 0 (1st) element of the table
    {
        cout << table[tab] << " "; //write the value of an element (0 or 1)
        tab--; //decreasing by 1 so we show 0's and 1's FROM THE BACK (correct way)
    }

    cout << endl;
    return 0;
}

顺便说一句,这很复杂,但我已经尽力了.

By the way it's complicated but I tried my best.

编辑 - 这是我最终使用的解决方案:

std::string toBinary(int n)
{
    std::string r;
    while(n!=0) {r=(n%2==0 ?"0":"1")+r; n/=2;}
    return r;
}

推荐答案

std::bitset 有一个 .to_string() 方法,该方法返回一个 std::string 以二进制形式保存文本表示,前导零填充.

std::bitset has a .to_string() method that returns a std::string holding a text representation in binary, with leading-zero padding.

根据数据的需要选择位集的宽度,例如std::bitset<32> 从 32 位整数中获取 32 个字符的字符串.

Choose the width of the bitset as needed for your data, e.g. std::bitset<32> to get 32-character strings from 32-bit integers.

#include <iostream>
#include <bitset>

int main()
{
    std::string binary = std::bitset<8>(128).to_string(); //to binary
    std::cout<<binary<<"
";

    unsigned long decimal = std::bitset<8>(binary).to_ulong();
    std::cout<<decimal<<"
";
    return 0;
}

请不要编辑我对八进制和十六进制的回答.OP 专门要求十进制转二进制.

Please do not edit my answer for Octal and Hexadecimal. The OP specifically asked for Decimal To Binary.

这篇关于C++ - 十进制到二进制转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:C++ - 十进制到二进制转换

基础教程推荐