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
Binary String to Integer with 'atoi()'
提问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
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.
atoi
不处理二进制数,它只是将它们解释为大十进制数。您的问题是它太高了,并且由于它被解释为十进制数而导致整数溢出。
The solution would be to use stoi
, stol
or stoll
that got added to string
in C++11. Call them like
解决方案是使用stoi
,stol
或者在 C++11 中stoll
添加string
。像这样称呼他们
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..
- 返回值是转换后的
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: 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.
请注意,还有 C++11 之前的函数具有几乎相同的名称,例如:strtol
与 C++11 相比stol
。
它们也适用于不同的基础,但它们不会以相同的方式进行错误处理(它们尤其缺乏在给定字符串上根本无法进行转换的情况下,例如尝试将“hello”转换为字符串)并且您应该更喜欢 C++11 版本。
To make my point, passing "Hello" to both strtol
and the C++11 stol
would lead to:
为了说明我的观点,将“Hello”传递给strtol
C++11 和 C++11stol
会导致:
strtol
returns0
and doesn't give you any way to identify it as error,stol
from C++11 throwsstd::invalid_argument
and 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 页面的链接。