C++ - Decimal to binary converting

后端 未结 29 3468
一向
一向 2020-11-28 18:53

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:<

相关标签:
29条回答
  • 2020-11-28 19:48
    #include <iostream>
    #include <bitset>
    
    #define bits(x)  (std::string( \
                std::bitset<8>(x).to_string<char,std::string::traits_type, std::string::allocator_type>() ).c_str() )
    
    
    int main() {
    
       std::cout << bits( -86 >> 1 ) << ": " << (-86 >> 1) << std::endl;
    
       return 0;
    }
    
    0 讨论(0)
  • 2020-11-28 19:49

    An int variable is not in decimal, it's in binary. What you're looking for is a binary string representation of the number, which you can get by applying a mask that filters individual bits, and then printing them:

    for( int i = sizeof(value)*CHAR_BIT-1; i>=0; --i)
        cout << value & (1 << i) ? '1' : '0';
    

    That's the solution if your question is algorithmic. If not, you should use the std::bitset class to handle this for you:

    bitset< sizeof(value)*CHAR_BIT > bits( value );
    cout << bits.to_string();
    
    0 讨论(0)
  • 2020-11-28 19:51

    You can use std::bitset to convert a number to its binary format.

    Use the following code snippet:

    std::string binary = std::bitset<8>(n).to_string();
    

    I found this on stackoverflow itself. I am attaching the link.

    0 讨论(0)
  • 2020-11-28 19:51

    You want to do something like:

    cout << "Enter a decimal number: ";
    cin >> a1;
    cout << setbase(2);
    cout << a1
    
    0 讨论(0)
  • 2020-11-28 19:51
    #include <iostream>
    
    // x is our number to test
    // pow is a power of 2 (e.g. 128, 64, 32, etc...)
    int printandDecrementBit(int x, int pow)
    {
        // Test whether our x is greater than some power of 2 and print the bit
        if (x >= pow)
        {
            std::cout << "1";
            // If x is greater than our power of 2, subtract the power of 2
            return x - pow;
        }
        else
        {
            std::cout << "0";
            return x;
        }
    }
    
    int main()
    {
        std::cout << "Enter an integer between 0 and 255: ";
        int x;
        std::cin >> x;
    
        x = printandDecrementBit(x, 128);
        x = printandDecrementBit(x, 64);
        x = printandDecrementBit(x, 32);
        x = printandDecrementBit(x, 16);
    
        std::cout << " ";
    
        x = printandDecrementBit(x, 8);
        x = printandDecrementBit(x, 4);
        x = printandDecrementBit(x, 2);
        x = printandDecrementBit(x, 1);
    
        return 0;
    }
    

    this is a simple way to get the binary form of an int. credit to learncpp.com. im sure this could be used in different ways to get to the same point.

    0 讨论(0)
  • 2020-11-28 19:51
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <math.h>
    
    void Decimal2Binary(long value,char *b,int len)
    {
        if(value>0)
        {
            do
            {
                if(value==1)
                {
                    *(b+len-1)='1';
                    break;
                }
                else
                {
                    *(b+len-1)=(value%2)+48;
                    value=value/2;
                    len--;
                }
            }while(1);
        }
    }
    long Binary2Decimal(char *b,int len)
    {
        int i=0;
        int j=0;
        long value=0;
        for(i=(len-1);i>=0;i--)
        {
            if(*(b+i)==49)
            {
                value+=pow(2,j);
            }
            j++;
        }
        return value;
    }
    int main()
    {
        char data[11];//最後一個BIT要拿來當字串結尾
        long value=1023;
        memset(data,'0',sizeof(data));
        data[10]='\0';//字串結尾
        Decimal2Binary(value,data,10);
        printf("%d->%s\n",value,data);
        value=Binary2Decimal(data,10);
        printf("%s->%d",data,value);
        return 0;
    }
    
    0 讨论(0)
提交回复
热议问题