c++ 将整数转换为字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37838859/
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
Converting integer to string in c++
提问by Gibreel Abdullah
This is the code I wrote to convert integer to string.
这是我编写的将整数转换为字符串的代码。
#include <iostream>
using namespace std;
int main()
{
string s;
int b=5;
s.push_back((char)b);
cout<<s<<endl;
}
I expected the output to be 5 but it is giving me blank space.
我预计输出为 5,但它给了我空白空间。
I know there is another way of doing it using stringstream but I want to know what is wrong in this method?
我知道还有另一种使用 stringstream 的方法,但我想知道这种方法有什么问题?
回答by MikeCAT
Character code for numbers are not equal to the integer the character represents in typical system.
数字的字符代码不等于字符在典型系统中代表的整数。
It is granteed that character codes for decimal digits are consecutive (N33372.3 Character sets, Paragraph 3), so you can add '0'
to convert one-digit number to character.
允许十进制数字的字符代码是连续的(N33372.3 字符集,第3段),因此您可以添加'0'
将一位数字转换为字符。
#include <iostream>
using namespace std;
int main()
{
string s;
int b=5;
s.push_back((char)(b + '0'));
cout<<s<<endl;
}
回答by Sebastian Hoffmann
回答by Mattia F.
The problem in your code is that you are converting the integer 5 to ASCII (=> ENQ ASCII code, which is not "printable"). To convert it to ASCII properly, you have to add the ASCII code of '0' (48), so:
您的代码中的问题是您将整数 5 转换为 ASCII(=> ENQ ASCII 代码,不可“打印”)。要将其正确转换为 ASCII,您必须添加 '0' (48) 的 ASCII 代码,因此:
char ascii = b + '0';
However, to convert an integer to std::string use:
但是,要将整数转换为 std::string 使用:
std::stringstream ss; //from <sstream>
ss << 5;
std::string s = ss.str ();
I always use this helper function in my projects:
我总是在我的项目中使用这个辅助函数:
template <typename T>
std::string toString (T arg)
{
std::stringstream ss;
ss << arg;
return ss.str ();
}
回答by Benjamin Navarro
As others said, you can't convert an integer to a string the way you are doing it.
正如其他人所说,您不能按照您的方式将整数转换为字符串。
IMHO, the best way to do it is using the C++11 method std::to_string.
恕我直言,最好的方法是使用 C++11 方法std::to_string。
Your example would translate to:
您的示例将转换为:
using namespace std;
int main()
{
string s;
int b=5;
s = to_string(b);
cout<<s<<endl;
}
回答by Zeps
Also, you can use stringstream, std::to_string doesn't work for me on GCC
此外,您可以使用stringstream, std::to_string 在 GCC 上对我不起作用
回答by Francesco Dondi
If we were writing C++ from scratch in 2016, maybe we would make this work. However as it choose to be (mostly) backward compatible with a fairly low level language like C, 'char'
is in fact just a number, that string/printing algorithms interpret as a character -but most of the language doesn't treat special. Including the cast. So by doing (char)
you're only converting a 32 bit signed number (int
) to a 8 bit signed number (char
).
如果我们在 2016 年从头开始编写 C++,也许我们会成功。然而,因为它选择(大部分)向后兼容像 C 这样的相当低级的语言'char'
,实际上只是一个数字,字符串/打印算法解释为一个字符 - 但大多数语言并不特殊对待。包括演员阵容。因此,这样做(char)
只是将 32 位有符号数 ( int
) 转换为 8 位有符号数 ( char
)。
Then you interpret it as a character when you print it, since printing functions do treat it special. But the value it gets printed to is not '5'
. The correspondence is conventional and completely arbitrary; the first numbers were reserved to special codes which are probably obsolete by now. As Hoffman pointed out, the bit value 5
is the code for Enquiry
(whatever it means), while to print '5'
the character has to contain the value 53
. To print a proper space you'd need to enter 32
. It has no meaning other than someone decided this was as good as anything, sometime decades ago, and the convention stuck.
然后在打印时将其解释为字符,因为打印函数确实将其特殊对待。但是它打印的值不是'5'
. 对应关系是约定俗成的,完全是随意的;第一个数字保留给特殊代码,这些代码现在可能已经过时了。正如霍夫曼指出的那样,位值5
是Enquiry
(无论它意味着什么)的代码,而打印'5'
字符必须包含值53
。要打印适当的空间,您需要输入32
. 除了几十年前有人认为这与任何事情一样好之外,它没有任何意义,并且约定俗成。
If you need to know for other characters and values, what you need is an "ASCII table". Just google it, you'll find plenty.
如果您需要了解其他字符和值,您需要的是“ASCII 表”。谷歌一下,你会发现很多。
You'll notice that numbers and letters of the same case are next to each other in the order you expect, so there is some logic to it at least. Beware, however, it's often not intuitive anyway: uppercase letters are before lowercase ones for instance, so 'A' < 'a'
.
您会注意到同一个大小写的数字和字母按照您期望的顺序彼此相邻,因此至少有一些逻辑。但是,请注意,它通常并不直观:例如,大写字母在小写字母之前,因此'A' < 'a'
.
I guess you're starting to see why it's better to rely on dedicated system functions for strings!
我想您已经开始明白为什么最好依靠专用的系统函数来处理字符串了!