-1

I want to convert binary count to decimal. My code snippet :

int main(){
    string count;
    char number;
    int counter;
    int score = 0;

    cout << "Wpisz liczbę w systemie binarnym" << endl;
    cin >> count;
    count << endl;

    counter = count.length();

    for (int i = 0; i < counter; i++)
    {
        number = count[i];
        int aaa = (int) number - '0' // is that correct ? I think not.
        score = aaa * pow(2,i) + score;
    }

    count <<wynik;
    return 0;
}

But my programm gives very strange scores, for example :

input : 100 (decimal 8) output : 1

input 1000 (decimal 16) output : 1

I don't know why. Can you help me ? I think, that that is the problem with char ? Am I true ?

Thanks,

Sorry for my Englosh

TN888
  • 7,659
  • 9
  • 48
  • 84
  • 2
    You should apologize for your indentation too :-) – Pete Fordham May 01 '13 at 17:16
  • The first character in the string `"100"` is `'1'`, so the first assignment to `wynik` in the loop is `1 * pow(2,0) + 0`. – dyp May 01 '13 at 17:18
  • 1
    You may want to check out [`std::stoi`](http://en.cppreference.com/w/cpp/string/basic_string/stol) or [`std::strtol`](http://en.cppreference.com/w/cpp/string/byte/strtol). – Some programmer dude May 01 '13 at 17:18
  • English variable names are nice to help others understand your code :) – dyp May 01 '13 at 17:19
  • Your code is missing a semicolon and misspelled `cout` twice. Here's the code anglicised: http://coliru.stacked-crooked.com/view?id=5ace49c9f6e5d34b2dcd2f2e33e0c58d-50d9cfc8a1d350e7409e81e87c2653ba – Mooing Duck May 01 '13 at 17:20
  • atoi or an appropriate C++ converter function that I am not aware of please. – Michael Dorgan May 01 '13 at 17:20
  • 1
    @pytajnik: Actually, studying your code, it's _amazingly_ close to correct! the only problem is you are parsing the digits in reverse. You're doing the math for `1000` as if it were `0001`. And that in binary converted to decimal is indeed `1`. – Mooing Duck May 01 '13 at 17:21
  • To add another solution: "cin >> setbase(2) >> wynik;` does all the conversion internally :) – dyp May 01 '13 at 17:22
  • Can you give me code ? I see here a lot of soulutions, but I can't use it :/ – TN888 May 01 '13 at 17:23
  • If you just want to read an integer in binary code from `cin`, place `#include ` and `#include ` at the beginning you your file (and somewhere `using namespace std;`), then just read from `cin` via `cin >> setbase(2) >> wynik;`. [`setbase`](http://en.cppreference.com/w/cpp/io/manip/setbase) changes the base from 10 (default) to 2 here, you can use 16 as well for hexadecimal. – dyp May 01 '13 at 17:42
  • It doesn't work, sorry :/ – TN888 May 01 '13 at 19:01

2 Answers2

1

Give this a try:

int main(){
    string liczba;
    int licznik;
    int wynik = 0;

    cout << "Wpisz liczb w systemie binarnym" << endl;
    cin >> liczba;

    licznik = liczba.length();

    for (int i = 0; i < licznik; i++)
    {
        int aaa = liczba[i] -'0'; // A bit hacky way to convert char to int
        wynik += aaa * pow(2.0,licznik - i - 1); // Minus 1 to get the correct power
    }
    cout << wynik;
    return 0;
}

Not sure where you got count from in your code, but I assume you actually meant cout.
See this question for more on char conversion.

Also, as others have pointed out, when reading the chars out of a string starting with a 0 index, you'll be pulling out binary digits of decreasing significance. That means your exponent should be decreasing, not increasing.

Edit: I also noticed in your question you are reading binary incorrectly:

input : 100 (decimal 8) output : 1
input 1000 (decimal 16) output : 1

100 is 4, not 8. Likewise, 1000 is 8, not 16. Perhaps you're forgetting 2^0, or the 1's digit?

Community
  • 1
  • 1
eriknelson
  • 849
  • 2
  • 11
  • 21
1

Another way is to get rid of the pow which involves cast to the from double.

int main(){
    string liczba;
    int licznik;
    int wynik = 0;

    cout << "Wpisz liczb w systemie binarnym" << endl;
    cin >> liczba;

    licznik = liczba.length();

    for (int i = 0; i < licznik; i++)
    {
        int aaa = liczba[i] -'0'; // A bit hacky way to convert char to int
        wynik *= 2;   // shift existing total up a power of 2 (could use << 1) 
        wynik += aaa; // Add the next digit
    }
    cout << wynik;
    return 0;
}
parkydr
  • 7,596
  • 3
  • 32
  • 42