Convert String to int / Convert int to String in C++

Published on
38,049 Points
6 Endorsements
Last Modified:
This EE article provides examples of conversion techniques for various types of string variables:  classic C-style strings, MFC/ATL CStrings, and STL's std::string data type.

Convert Integer to String

int to C-style (NULL-terminated char[]) string
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

Open in new window

int to ATL/MFC CString
#include <cstringt.h>
int     n;
CString sResult;

sResult.Format( "%d", n );    // use "%u" for unsigned int

Open in new window

int to STL std::string
#include <iostream>
#include <sstream>
int         n;
std::string sResult;

std::ostringstream ostr;
ostr << n;
sResult= ostr.str();

Open in new window

Convert String to Integer

C-style (NULL-terminated char[]) string to int
#include <stdlib.h>
char szNumber[12]= "1234567890";
int  nResult;

nResult= atoi( szNumber );

Open in new window

ATL/MFC CString to int
#include <cstringt.h>  
CString sNumber= "1234567890";
int     nResult;

nResult= atoi( sNumber );  // automatically does LPCSTR(sNumber)

Open in new window

STL std::string to int
#include <string>
#include <stdlib.h>
std::string sNumber= "1234567890";
int         nResult;

nResult= atoi( sNumber.c_str() );

Open in new window

If you are using the UNICODE character set and supplying an output buffer (as with sprintf and itoa) you'll need to keep in mind that characters are two bytes long.  The normal procedure is to declare character buffers as the TCHAR data type, which will take into consideration the data element size.
Output buffer length.
C++ integers are typically 32-bits, with values as high as billions; the range is: 0 to 4,294,967,295 (unsigned) -2,147,483,648 to 2,147,483,647 (signed)Thus, the maximum length of the resulting string is 12 characters (including NULL terminator and not including commas or other formatting).

If you are working with 64-bit integers (called __int64 or long long), the values are as high as quintillions; the range is: 0 to 18,446,744,073,709,551,615 (unsigned) -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 (signed)Thus, the maximum length of the resulting string is 21 characters (including NULL terminator and not including commas or other formatting).
Both sprintf and itoa have been termed unsafe (some would say they've been deprecated, others would not use that term) because of the chance that a sloppy programmer might not provide a large enough output buffer.

The examples show how to use the safer xxx_s variations of these functions as an alternative.  The older functions might write beyond the end of the buffer and stomp on other variables or blow the stack frame -- and cause endless debugging headaches.  Of course, if you are determined to give yourself grief, you can still blow up the "safe" version by passing in the wrong length value...  

The CString::Format function allocates the buffer for you and takes care to avoid the buffer overrun problem.  The std::ostringstream << (insertion operator) also takes care of the buffer allocation for you.
The examples above compile and work under Microsoft VS2008.  Some Microsoft-specific functionality is implied (refer to the references, below, if you worry about these things).  However, there is an excellent chance that at least one of the variations will work for you in your development system, whatever it is.


atoi, _atoi_l, _wtoi, _wtoi_l

sprintf, _sprintf_l, swprintf, _swprintf_l, __swprintf_l

_itoa, _i64toa, _ui64toa, _itow, _i64tow, _ui64tow

String to Numeric Value Functions  (strtoX, et al.)

Data Conversion  (ultoX, etc.)



If you liked this article and want to see more from this author,  please click the Yes button near the:
      Was this article helpful?
label that is just below and to the right of this text.   Thanks!
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Featured Post

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Join & Write a Comment

The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month