简体   繁体   English

在C ++中将整数转换为字符串

[英]Converting integer to string in c++

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的方法,但是我想知道此方法有什么问题?

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 ( N3337 2.3 Character sets, Paragraph 3), so you can add '0' to convert one-digit number to character. 允许十进制数字的字符代码是连续的( N3337 2.3字符集,第3段),因此您可以添加'0'以将一位数字转换为字符。

#include <iostream>
using namespace std;
int main()
{
    string s;
    int b=5;
    s.push_back((char)(b + '0'));
    cout<<s<<endl;
}

You are interpreting the integer 5 as a character. 您正在将整数5解释为字符。 In ASCII encoding, 5 is the Enquiry control character as you lookup here . 在ASCII编码中,当您在此处查找时, 5查询 控制字符

The character 5 on the other hand is represented by the decimal number 53. 另一方面,字符5由十进制数字53表示。

The problem in your code is that you are converting the integer 5 to ASCII (=> ENQ ASCII code, which is not "printable"). 您的代码中的问题是您正在将整数5转换为ASCII(=> ENQ ASCII代码,这是“不可打印的”)。 To convert it to ASCII properly, you have to add the ASCII code of '0' (48), so: 要将其正确转换为ASCII,必须添加ASCII代码“ 0”(48),因此:

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 ();
}

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;
}

另外,您可以使用stringstream ,std :: to_string在GCC上对我不起作用

If we were writing C++ from scratch in 2016, maybe we would make this work. 如果我们在2016年从头开始编写C ++,也许我们会做的。 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. 但是,由于它选择(主要)与诸如C之类的相当低级的语言向后兼容,因此'char'实际上只是一个数字,字符串/打印算法将其解释为字符-但大多数语言都不会将其视为特殊字符。 Including the cast. 包括演员表。 So by doing (char) you're only converting a 32 bit signed number ( int ) to a 8 bit signed number ( 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' . 但是打印出来的值不是'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 . 正如霍夫曼指出的那样,位值5是用于Enquiry的代码(无论其含义如何),而要打印'5'则字符必须包含值53 To print a proper space you'd need to enter 32 . 要打印适当的空间,您需要输入32 It has no meaning other than someone decided this was as good as anything, sometime decades ago, and the convention stuck. 除了有人在几十年前的某个时候认为这和任何事情一样好,而且惯例被搁置之外,它没有其他意义。

If you need to know for other characters and values, what you need is an "ASCII table". 如果需要了解其他字符和值,则需要一个“ ASCII表”。 Just google it, you'll find plenty. 只是谷歌,你会发现很多。

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! 我猜您已经开始明白为什么最好使用专用的系统函数来处理字符串了!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM