int n=1234567890;
char szResult[12];
#include <stdio.h>
...
sprintf( szResult, "%d", n ); // use "%u" for unsigned int
sprintf_s( szResult, sizeof(szResult), "%d", n ); // "safe" version
//------------------------------------- alternative: itoa
#include <stdlib.h>
...
_itoa( n, szResult, 10 );
_itoa_s( n, szResult, sizeof(szResult), 10); // "safe" version
int to ATL/MFC CString
#include <cstringt.h>
...
int n;
CString sResult;
sResult.Format( "%d", n ); // use "%u" for unsigned int
int to STL std::string
#include <iostream>
#include <sstream>
...
int n;
std::string sResult;
std::ostringstream ostr;
ostr << n;
sResult= ostr.str();
#include <stdlib.h>
...
char szNumber[12]= "1234567890";
int nResult;
nResult= atoi( szNumber );
ATL/MFC CString to int
#include <cstringt.h>
...
CString sNumber= "1234567890";
int nResult;
nResult= atoi( sNumber ); // automatically does LPCSTR(sNumber)
STL std::string to int
#include <string>
#include <stdlib.h>
...
std::string sNumber= "1234567890";
int nResult;
nResult= atoi( sNumber.c_str() );
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (0)