使用'atoi()'将二进制字符串转换为整数 [英] Binary String to Integer with 'atoi()'

查看:333
本文介绍了使用'atoi()'将二进制字符串转换为整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个二进制字符串,然后使用atoi()转换为整数.当我这样做时,似乎会自动将二进制转换为十进制.问题在于所得的整数为负,并且与任何在线二进制到十进制转换器不一致. atoi()是否有问题?我应该改用其他功能吗?

代码:

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

谢谢

解决方案

atoi不处理二进制数,它只是将它们解释为大十进制数.您的问题是它太高,由于将其解释为十进制数,因此会出现整数溢出.

解决方案是使用在C ++ 11中添加到stringstoistolstoll.称呼他们

int i = std::stoi("01000101", nullptr, 2);

  • 返回的值是转换后的int值.
  • 第一个参数是要转换的std::string.
  • 第二个是size_t *,它将保存第一个非数字字符的索引.
  • 第三个是int,它对应于将用于转换的基数.

有关这些功能的信息,请参见其cppreference页面./p>


请注意,还有一些C ++ 11之前的函数具有几乎相同的名称,例如:strtol与C ++ 11 stol相比.
它们也适用于不同的基础,但是它们不会以相同的方式进行错误处理(当根本无法在给定的字符串上进行任何转换(例如,尝试将"hello"转换为字符串)时,它们尤其如此)和您可能应该更喜欢C ++ 11版本.

为了说明我的观点,将"Hello"同时传递给strtol和C ++ 11 stol会导致:

  • strtol返回0,并且没有任何方式将其识别为错误,
  • C ++ 11中的
  • stol会抛出std::invalid_argument并表明有问题.

错误地将"Hello"之类的内容解释为整数可能会导致bug,在我看来应该避免.

但出于完整性考虑,也链接到其cpp引用页面.

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

解决方案

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.

这篇关于使用'atoi()'将二进制字符串转换为整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆