20

I have a string of binary that I then convert to an integer using atoi(). When I do this it seems to automatically convert the binary to decimal. The issue is that the resulting integer is negative and doesn't agree with any of the online binary-to-decimal converters. Is something broken with atoi()? Should I be using a different function instead?

Code:

string myString = "01000101";
int x = atoi(myString.c_str());
cout << x;

Thanks

SkippyNBS
  • 687
  • 1
  • 5
  • 21
  • What exactly do you mean by *a string of binary*? Something like `"10001"` that you need to convert to `17`? – NPE May 11 '14 at 19:33
  • Ya, I just edited it to make it a little more clear – SkippyNBS May 11 '14 at 19:35
  • `atoi()` treats the input string as decimal; you could use `strtol()` (or one of its relatives) with `2` as the base to get binary. – Jonathan Leffler May 11 '14 at 19:44
  • 1
    @JonathanLeffler `stol` should be used nowadays (if you got C++11 available) in my opinion, it got better errorhandling. For example passing something like "hello" to `strtol` would return `0` passing it to the C++11 `stol` would throw `std::invalid_argument`. Interpreting "Hello" as `0` might lead to many bugs. – AliciaBytes May 11 '14 at 19:59
  • 1
    Fair enough. `strtol()` reports errors in circuitous ways and basically shouldn't be called directly if you're serious about error detection. You'd wrap it in a function that spots the errors that you care about and reports them in an appropriate manner. It can be used when you don't mind too much about the error processing. GIGO (garbage in, garbage out) is still valid; you may or may not want to detect the GI part if the GO part won't do much damage. One advantage of `strtol()` is that it is from C and is always available — it doesn't rely on C++11. Whether that matters depends on context. – Jonathan Leffler May 11 '14 at 20:05

2 Answers2

48

atoi doesn't handle binary numbers, it just interprets them as big decimal numbers. Your problem is that it's too high and you get an integer overflow due to it being interpreted as decimal number.

The solution would be to use stoi, stol or stoll that got added to string in C++11. Call them like

int i = std::stoi("01000101", nullptr, 2);
  • The returned value is the converted int value.
  • The first argument is the std::string you want to convert.
  • The second is a size_t * where it'll save the index of the first non digit character.
  • The third is an int that corresponds to the base that'll be used for conversion..

For information on the functions look at its cppreference page.


Note that there are also pre C++11 functions with nearly the same name, as example: strtol compared to the C++11 stol.
They do work for different bases too, but they don't do the error handling in the same way (they especially lack when no conversion could be done on the given string at all e.g trying to convert "hello" to a string) and you should probably prefer the C++11 versions.

To make my point, passing "Hello" to both strtol and the C++11 stol would lead to:

  • strtol returns 0 and doesn't give you any way to identify it as error,
  • stol from C++11 throws std::invalid_argument and indicates that something is wrong.

Falsely interpreting something like "Hello" as integers might lead to bugs and should be avoided in my opinion.

But for completeness sake a link to its cppreference page too.

Dodgie
  • 643
  • 1
  • 10
  • 17
AliciaBytes
  • 7,300
  • 6
  • 36
  • 47
6

It sounds like you should be using strtol() with 2 as the last argument.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • Since this is the accepted answer I'll just add that I think the C++11 versions for string to integer conversions are supperior when it comes to error handling (and `strtol` shouldn't be used). More information at the bottom of my answer. – AliciaBytes May 11 '14 at 20:05