Binary String to Integer with 'atoi()'

40,100

Solution 1

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.

Solution 2

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

Share:
40,100

Related videos on Youtube

SkippyNBS
Author by

SkippyNBS

Updated on July 09, 2022

Comments

  • SkippyNBS
    SkippyNBS almost 2 years

    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

    • NPE
      NPE almost 10 years
      What exactly do you mean by a string of binary? Something like "10001" that you need to convert to 17?
    • SkippyNBS
      SkippyNBS almost 10 years
      Ya, I just edited it to make it a little more clear
    • Jonathan Leffler
      Jonathan Leffler almost 10 years
      atoi() treats the input string as decimal; you could use strtol() (or one of its relatives) with 2 as the base to get binary.
    • AliciaBytes
      AliciaBytes almost 10 years
      @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.
    • Jonathan Leffler
      Jonathan Leffler almost 10 years
      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.
  • AliciaBytes
    AliciaBytes almost 10 years
    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.