C++ 使用 'atoi()' 将二进制字符串转换为整数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23596988/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 00:29:54  来源:igfitidea点击:

Binary String to Integer with 'atoi()'

c++binarydecimalatoi

提问by SkippyNBS

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?

我有一个二进制字符串,然后我使用atoi(). 当我这样做时,它似乎会自动将二进制转换为十进制。问题是生成的整数是负数,并且与任何在线二进制到十进制转换器不符。东西坏了atoi()吗?我应该使用不同的功能吗?

Code:

代码:

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

Thanks

谢谢

回答by user1942027

atoidoesn'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.

atoi不处理二进制数,它只是将它们解释为大十进制数。您的问题是它太高了,并且由于它被解释为十进制数而导致整数溢出。

The solution would be to use stoi, stolor stollthat got added to stringin C++11. Call them like

解决方案是使用stoistol或者在 C++11 中stoll添加string。像这样称呼他们

int i = std::stoi("01000101", nullptr, 2);
  • The returned value is the converted intvalue.
  • The first argument is the std::stringyou 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 intthat corresponds to the base that'll be used for conversion..
  • 返回值是转换后的int值。
  • 第一个参数是std::string您要转换的。
  • 第二个是size_t *保存第一个非数字字符的索引的地方。
  • 第三个是int对应于将用于转换的基数..

For information on the functions look at its cppreference page.

有关这些功能的信息,请查看其 cppreference 页面



Note that there are also pre C++11 functions with nearly the same name, as example: strtolcompared 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.

请注意,还有 C++11 之前的函数具有几乎相同的名称,例如:strtol与 C++11 相比stol
它们也适用于不同的基础,但它们不会以相同的方式进行错误处理(它们尤其缺乏在给定字符串上根本无法进行转换的情况下,例如尝试将“hello”转换为字符串)并且您应该更喜欢 C++11 版本

To make my point, passing "Hello" to both strtoland the C++11 stolwould lead to:

为了说明我的观点,将“Hello”传递给strtolC++11 和 C++11stol会导致:

  • strtolreturns 0and doesn't give you any way to identify it as error,
  • stolfrom C++11 throws std::invalid_argumentand indicates that something is wrong.
  • strtol返回0并且没有给您任何方式将其识别为错误,
  • stol从 C++11 抛出std::invalid_argument并表明有问题。

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

错误地将诸如“Hello”之类的内容解释为整数可能会导致错误,在我看来应该避免。

But for completeness sake a link to its cppreference pagetoo.

但为了完整起见,也有指向其 cppreference 页面的链接。

回答by NPE

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

听起来您应该使用strtol()with2作为最后一个参数。