visual c++: convert int into string pointer

前端 未结 10 2123
既然无缘
既然无缘 2021-01-22 09:01

how to convert integer into string pointer in visual c++?

相关标签:
10条回答
  • 2021-01-22 09:38

    This is how I did it in my homework since we were only allowed to use some predetermined libraries. I'm pretty sure it's not considered a "best practice" though ;)

    string int2string(int integer) {
        string str;
        int division = integer;
    
        while (division > 0) {
            str = char('0' + (division % 10)) + str;
            division = division / 10;
        }
    
        return str;
    }
    
    0 讨论(0)
  • 2021-01-22 09:42

    search for atoi / itoa in your favorite documentation. Or try Boost (www.boost.org - library Conversion, lexical_cast).

    Both ways are portable across different compilers.

    0 讨论(0)
  • 2021-01-22 09:44

    Use stringstream

    #include <sstream>
    stringstream ss;
    ss << i;
    string   s = ss.str();
    
    0 讨论(0)
  • 2021-01-22 09:45

    If you using CString, then you can use Format() method like this:

    int val = 489;
    CString s;
    s.Format("%d", val);
    
    0 讨论(0)
提交回复
热议问题