C++ 中的 Char 到 Int?
可能的重复:
如何将单个 char 转换为 int < /p>
嗯,我正在做一个基本程序,它处理一些输入,例如:
2+2
所以,我需要添加 2 + 2。
我做了类似的事情:
string mys "2+2";
fir = mys[0];
sec = mys[2];
但现在我想将“fir”添加到“sec”,所以我需要转换他们到国际。 我尝试了“int(fir)”但没有成功。
Possible Duplicate:
How to convert a single char into an int
Well, I'm doing a basic program, wich handles some input like:
2+2
So, I need to add 2 + 2.
I did something like:
string mys "2+2";
fir = mys[0];
sec = mys[2];
But now I want to add "fir" to "sec", so I need to convert them to Int.
I tried "int(fir)" but didn't worked.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将字符串转换为 int 的方法有多种。
解决方案 1:使用旧版 C 功能
解决方案 2:使用
lexical_cast
(最合适且最简单)解决方案 3:使用
C++ Streams
There are mulitple ways of converting a string to an int.
Solution 1: Using Legacy C functionality
Solution 2: Using
lexical_cast
(Most Appropriate & simplest)Solution 3: Using
C++ Streams
好吧,首先介绍一下为什么您尝试的方法不起作用。在您的示例中,fir 被声明为字符串。当您尝试执行与 (int)fir 相同的 int(fir) 时,您尝试了从字符串到整数的 C 风格转换。本质上你会得到垃圾,因为 c++ 中的 c 风格转换将遍历所有可用的转换并采用第一个有效的转换。最多您将获得代表字符 2 的内存值,这取决于您使用的字符编码(UTF-8、ascii 等...)。例如,如果 fir 包含“2”,那么您可能会得到 0x32 作为整数值(假设为 ascii)。你真的不应该使用 c 风格的强制转换,唯一真正安全使用它们的地方是数字类型之间的转换。
如果您给定的字符串与示例中的字符串类似,那么首先您应该使用 strtok 等函数将字符串分成相关的字符序列(标记)。在这个简单的例子中,这将是“2”、“+”和“2”。完成此操作后,您可以简单地对要转换为整数的字符串调用函数,例如 atoi。
示例:
但是,如果您也希望能够处理非整数,这会变得稍微复杂一些。在这种情况下,最好的办法是分隔操作数(+ - / * 等),然后在数字字符串上查找小数点。如果你找到了,你可以将它视为双精度,并使用函数 atof 而不是 atoi,如果没有,就坚持使用 atoi。
Alright so first a little backround on why what you attempted didn't work. In your example, fir is declared as a string. When you attempted to do int(fir), which is the same as (int)fir, you attempted a c-style cast from a string to an integer. Essentially you will get garbage because a c-style cast in c++ will run through all of the available casts and take the first one that works. At best your going to get the memory value that represents the character 2, which is dependent upon the character encoding your using (UTF-8, ascii etc...). For instance, if fir contained "2", then you might possibly get 0x32 as your integer value (assuming ascii). You should really never use c-style casts, and the only place where it's really safe to use them are conversions between numeric types.
If your given a string like the one in your example, first you should separate the string into the relevant sequences of characters (tokens) using a function like strtok. In this simple example that would be "2", "+" and "2". Once you've done that you can simple call a function such as atoi on the strings you want converted to integers.
Example:
However, this will get slightly more complicated if you want to be able to handle non-integer numbers as well. In that case, your best bet is to separate on the operand (+ - / * etc), and then do a find on the numeric strings for a decimal point. If you find one you can treat it as a double and use the function atof instead of atoi, and if you don't, just stick with atoi.
您是否尝试过 atoi 或 增强词法转换?
Have you tried atoi or boost lexical cast?