how to convert from int to char*?

前端 未结 10 2092
旧巷少年郎
旧巷少年郎 2020-11-30 18:37

The only way I know is:

#include 
#include 
using namespace std;

int main() {
  int number=33;
  stringstream strs;
  strs &l         


        
10条回答
  •  一向
    一向 (楼主)
    2020-11-30 19:17

    Converting our integer value to std::string so we can know how long (how long number of digits).

    Then we creating char array length of string letter size +1, so we can copy our value to string then char array.

    #include 
    
    char* intToStr(int data) {
        std::string strData = std::to_string(data);
    
        char* temp = new char[strData.length() + 1];
        strcpy(temp, strData.c_str());
    
       return temp;
    }
    

提交回复
热议问题