1. Using the Itoa function
Char *itoa(
int Value
, char *string,int radix); prototype Description:
value: The data that you want to convert.
string: The address of the target string.
Radix: Converted binary number, can be 2 binary, 8, 10, 16, etc. Exmaple:
#include <stdlib.h>#include<stdio.h>intMainvoid){ intNumber =12345; Char string[ +]; Itoa (number,string,Ten); printf ("integer =%d string =%s\n", number,string); return 0;}
2. Do not use library functions to convert to C-style strings
voidInttostring (intNumChar*s) { inti =0; while(num) {s[i]= num%Ten+'0'; Num= num/Ten; I++; } S[i]=' /'; for(intj =0, k = i1; J < K; j++,k--) { Chartemp =S[j]; S[J]=S[k]; S[K]=temp; }}
3. Using string streams to implement
string inttostring (int n) { ostringstream ostr; Ostr<<N; return ostr.str ();}
4. Using string implementations
string intToString2 (int n) { string""; while (n) { char c = n%'0'; = c+result; = n/; } return result;}
You can also use string to get the char* pointer
string str = inttostring (n); Char * s = const_cast<char*> (STR.C_STR ());
convert integers to Strings