Electickid2020
Electickid2020

Reputation: 31

c++ decimal to binary

I am a beginner starting out in c++ and I am trying to turn a decimal byte into a binary number. However, there is something wrong with my syntax or logic but I cannot figure it out. When trying to debug, I believe the error is around userValue5 but I'm not sure why. Any tips are appreciated and I am using VS2015.

    #include "stdafx.h"
    #include <iostream>
    #include <stdint.h>
    #include <cmath>

     //finds where each column is a 0 or a 1
    int binaryDigit(uint16_t y, uint16_t power)
    {
    if ((y / (pow(2, power))) > 1)
    return 1;
    else
    return 0;
    }

    int difference(uint16_t y, int x, uint16_t power)
    {
    if (x == 1)
    return y - pow(2, power);
    else
    return y;
    }

    //takes a decimal byte and turns it into binary
    int main()
    {
    using namespace std;
    cout << "Please insert a number between 0 and 255 so that I can convert it to binary: ";
    uint16_t userValue(0), power(7);
    cin >> userValue;

    int firstDigit = binaryDigit(userValue, power);
    uint16_t userValue2 = difference(userValue, firstDigit, power);
    --power;

    int secondDigit = binaryDigit(userValue2, power);
    uint16_t userValue3 = difference(userValue2, secondDigit, power);
    --power;

    int thirdDigit = binaryDigit(userValue3, power);
    uint16_t userValue4 = difference(userValue3, thirdDigit, power);
    --power;

    int fourthDigit = binaryDigit(userValue4, power);
    uint16_t userValue5 = difference(userValue4, thirdDigit, power);
    --power;

    int fifthDigit = binaryDigit(userValue5, power);
    uint16_t userValue6 = difference(userValue5, thirdDigit, power);
    --power;

    int sixthDigit = binaryDigit(userValue6, power);
    uint16_t userValue7 = difference(userValue6, thirdDigit, power);
    --power;

    int seventhDigit = binaryDigit(userValue7, power);
    uint16_t userValue8 = difference(userValue7, thirdDigit, power);
    --power;

    int eigthDigit = binaryDigit(userValue8, power);


    cout << "The number " << userValue << " in binary is ";
    cout << firstDigit << secondDigit << thirdDigit << fourthDigit << " " << fifthDigit << sixthDigit << seventhDigit << eigthDigit << endl;


    return 0;
    }

Upvotes: 2

Views: 225

Answers (3)

FluorescentGreen5
FluorescentGreen5

Reputation: 947

bitset is your friend.

#include <bitset>
#include <iostream>

using namespace std;

int main()
{
    int userValue = 0;

    cout << "Please insert a number between " << INT_MIN << " and " << INT_MAX << " so that I can convert it to binary: ";
    cin >> userValue;

    cout << bitset<32>(userValue) << endl;

    return 0;
}

However, if you want to convert it to a string, you'll probably need stringstream:

stringstream ss;
ss << bitset<32>(userValue);
string str = ss.str();

Upvotes: 2

Electickid2020
Electickid2020

Reputation: 31

I figured it out. When I copied and pasted the same code to save time, I forgot to edit the arguments so that's why it wasn't working properly. I fixed it and it works great. Thank you to everyone who commented and posted things like the table of exponents and the bit masking. I've learned a lot and cannot wait to write more programs.

Upvotes: 1

Peter Rindal
Peter Rindal

Reputation: 408

bitset is the way to go, but in general, maybe this is more helpful...

std::string intToBinaryString(int x)
{
    // a useful class that allows easy concatenation
    std::stringstream ss;


    // create an integer that only has a 1 bit in its top position.
    unsigned int mask = 1 << (sizeof(int) * 8 - 1);

    // for each bit in x, starting at the top, check to see if its non zero...

    for(int i = 0; i <sizeof(int) * 8; ++i)
    {
        // see if x's (top - i)'th bit is non zero
        bool nonZeroBit = x & mask;

        // assign the (top - i)'th "bit" of ss to be '1' if non zero and '0' otherwise
        ss <<  (nonZeroBit ? '1' : '0');

        // shift the mask down by 1.
        mask >>= 1;
    }

    // What we get on the first iteration is
    //
    //    mask   =   10000...0000
    //  &    x   =   10101...0010
    //  -----------------------------
    // nonZero   =   10000...0000 = true
    //
    // the second iteration is 
    //
    //    mask   =   01000...0000
    //  &    x   =   10101...0010
    //  -----------------------------
    // nonZero   =   00000...0000 = false
    //
    // the third iteration is 
    //
    //    mask   =   00100...0000
    //  &    x   =   10101...0010
    //  -----------------------------
    // nonZero   =   00100...0000 = true
    //
    // ...
    //
    // then we just add these results into ss 

    // finally, convert it into a normal string
    return ss.str();
}

Upvotes: 0

Related Questions