Binary to Decimal Converter

Hello everyone. I've been working on a binary to decimal converter, and it works for most numbers, but seems to fail at smaller numbers. For example, 10000 does not return 16.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <cmath>

using namespace std;

int main(){
    int bin, sum, byte;
    double x, exp;

    while(true){
            cout << endl << "Please enter the binary number: ";
            cin >> bin;
            byte = 0;
            sum = 0;
            x = 0;
            exp = 0;
            while((bin / (int) pow(10, x)) != 0)
                x++;
            for(exp; exp < x; exp++){
            byte = (bin / (int)pow(10, exp)) - (bin / ((int)pow(10, exp + 1)) * 10);

            if(byte == 1){
                sum += (int) pow(2, exp);
                }
            }

        cout << endl << bin << " in decimal is " << sum << ". " << endl;
        }
    cin.ignore();
    cin.get();
    return 0;
 }


Could someone explain what is causing this strange bug? Thanks.
IMHO there is a much easier approach.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream> //std::cout, std::cin, std::endl
#include <string> //std::string

//this approach is assuming that the binary number is unsigned and not signed.
int main()
{
    std::string binary = "";
    std::cout << "Please enter a binary number: ";
    std::cin >> binary;
    
    //auto is a c++11 feature
    auto rit = binary.rbegin(); //reverse iterator so we can iterate backwards
    unsigned bit = 1; //the value of the current bit
    unsigned decimal = 0; //we are hoping it is within range
    while(rit != binary.rend()) //iterate the entire string
    {
        if(*(rit++) == '1') //check if the current bit is set, then move to next bit
        {
            decimal += bit;
        }
        
        bit <<= 1; //shift it to the left one bit
    }
    
    std::cout << "Binary value: " << binary << '\n'
              << "Decimal value: " << decimal << std::endl;
              
    return 0;
}
There are obviously a large amount of very different ways to do this, I would however say that you are doing one of the more confusing ways.

By the way your code gave me 16 when I entered 10000 are you sure you built the newest version or input it correctly? Though they problems may be related to using doubles instead of pure ints. Doubles are not 100% accurate.
Last edited on
There is a built-in function that could be used for this, I show an example below
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>

int main()
{
    std::string foo = "011111001010";
    int bar {};
    try {
        bar = std::stoi( foo, nullptr, 2 );
    } catch ( ... ) {
        std::cout << "Invalid binary number" << std::endl;
    }
    std::cout << bar << std::endl;
    return 0;
}
I guess my way is pretty confusing, but it definitely does output 20 for 10000 when I compile my program in Code::Blocks.
There is a built-in function that could be used for this, I show an example below
There is also a std::bitset you can remove the leading zeros if you want but I didn't here is a quick sample:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>
#include <bitset>

int main()
{
    std::size_t const bits = 8u;
    std::bitset<bits> binary;
    std::cout << "Please enter a binary number(1 byte/8 bits): ";
    std::cin >> binary;
    
    std::cout << "Binary value: " << binary << std::endl;
    std::cout << "Decimal value: " << binary.to_ulong() << std::endl;
    
    return 0;
}



@OP
it definitely does output 20 for 10000 when I compile my program in Code::Blocks.
it is probably because of the doubles. They are not completely accurate. Try using integers only so it is more accurate. Though your program seems to "work" when you run it on cpp.sh.
Topic archived. No new replies allowed.