C/C++: Converting Strings to Numbers
Most input into your program, from command line arguments to reading in a text file to input fields in a GUI program, comes in as strings. Often we need to convert some of those strings into numerical data.
NOTE: All user input, whether you use it as strings or do other things with it, should be sanitized to make sure no one can hack your program. Always remember little Bobby Tables.
C functions for converting strings to numbers
The return type of the function is the kind of number it produces; remember that C “strings” are really arrays of char
with a last character of 0 ('\0'
in code). Arrays are passed by references so a parameter declared as char *s
is the same as a parameter declared as char s[]
.
int atoi(const char *nptr);
long atol(const char *nptr);
long long atoll(const char *nptr);
double atof(const char *nptr);
long int strtol(const char *nptr, char **endptr, int base);
long long int strtoll(const char *nptr, char **endptr, int base);
double strtod(const char *nptr, char **endptr);
float strtof(const char *nptr, char **endptr);
long double strtold(const char *nptr, char **endptr);
The functions are declared in the <stdlib.h>
header.
The names are meaningful even if they are a little cryptic. atoi
is
short for “alpha-to-integer”, and converts a string to an integer.
atol
converts to a long integer, and atof
converts to a floating
point number (double). The “a” functions aren’t very flexible and
have no way of communicating an error (such as maybe the string
doesn’t contain a number anyways), so the “str” functions are better.
They mean pretty much the same thing but their parameters allow you
to check for errors (see the man pages on how). Notice that there is
not a strtoi
function – that’s OK, you can use the strtol
and
then typecast is to an int.
C++ functions that convert C++ strings to numbers
int std::stoi( const std::string& str, std::size_t* pos = nullptr, int base = 10 );
long std::stol( const std::string& str, std::size_t* pos = nullptr, int base = 10 );
long long std::stoll( const std::string& str, std::size_t* pos = nullptr, int base = 10 );
unsigned long std::stoul( const std::string& str, std::size_t* pos = nullptr, int base = 10 );
unsigned long long std::stoull( const std::string& str, std::size_t* pos = nullptr, int base = 10 );
float std::stof( const std::string& str, std::size_t* pos = nullptr );
double std::stod( const std::string& str, std::size_t* pos = nullptr );
long double std::stold( const std::string& str, std::size_t* pos = nullptr );
These functions are declared in the <string>
header.
The C++ functions take the std::string
datatype by default, but you can
pass or promote a regular char*
C string into this by doing std::string(str)
, where str
is your plain C string variable. The second and third
parameters are optional (they have default values) and are only needed if
you want to convert in a base other than 10, or if you need to know where
the number stopped in a longer string (the pos argument). If you have a string str
that is a hexadecimal value, for example, you could do:
v = std::stoi(str,0,16);
Other resources
References pages at CPPReference: signed ints, unisgned ints, reals