Converting numbers to text and vice versa is a common issue as it can be useful in many different situations and C++98 doesn't provide a tool designed specifically to solve this problem.
Luckily C++ is a general purpose language so it allows to solve this quite easily and, as most things, you have many ways of accomplishing this task.
Here are listed some
Contents:
C++ - stringstreams
The
C++ stream library is powerful and it allows easy formatted input output operations. With
stringstreams you can perform this input/output to string, this allows you to convert numbers ( or any type with the
<<
>>
stream operators overloaded ) to and from strings.
With
stringstreams
you can use the same syntax to convert the different numeric types.
To use
stringstreams
you need to
#include <sstream>
Number to String
Converting a number to a string takes two steps using stringstreams:
- Outputting the value of the number to the stream
- Getting the string with the contents of the stream
As with this conversion needs only output operation with the stream, an
ostringstream
( output string stream ) can be used instead of the stream for both input and output (
stringstream
)
Here is an example which shows each step:
1
2
3
4
5
6
7
8
9
10
11
| int Number = 123; // number to be converted to a string
string Result; // string which will contain the result
ostringstream convert; // stream used for the conversion
convert << Number; // insert the textual representation of 'Number' in the characters in the stream
Result = convert.str(); // set 'Result' to the contents of the stream
// 'Result' now is equal to "123"
| |
This operation can be shorten into a single line:
1
2
| int Number = 123;
string String = static_cast<ostringstream*>( &(ostringstream() << Number) )->str();
| |
Here is constructed an unnamed stringstream object and performed the output
ostringstream() << Number
Then, since the << returns a reference to an
ostream
( a base of
ostringstream
) the result of the operation needs to be casted back to a stringstream
static_cast<ostringstream*>
Finally, we get the contents of the resulting stream as a string
->str()
and we assign that value to the string
string String =
Custom formatting
Stringstreams allow
manipulators and
locales to customize the result of these operations so you can easily change the format of the resulting string
Example: - This is not a complete program -
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
| // Headers needed:
#include <iomanip>
#include <locale>
#include <sstream>
#include <string> // this should be already included in <sstream>
// Defining own numeric facet:
class WithComma: public numpunct<char> // class for decimal numbers using comma instead of point
{
protected:
char do_decimal_point() const { return ','; } // change the decimal separator
};
// Conversion code:
double Number = 0.12; // Number to convert to string
ostringstream Convert;
locale MyLocale( locale(), new WithComma);// Crate customized locale
Convert.imbue(MyLocale); // Imbue the custom locale to the stringstream
Convert << fixed << setprecision(3) << Number; // Use some manipulators
string Result = Convert.str(); // Give the result to the string
// Result is now equal to "0,120"
| |
String to Number
Also converting a string to a number via stringstream takes two steps:
- Constructing the stream from the string
- Reading the value into the variable
For this ( as you need to read input from the stream ) an
istringstream
will be used
While a number can always be converted in a string, a string must be valid to be converted to a number ( eg: An attempt of converting
"hello"
to an integer would certainly fail ) so on this conversion, some checking must be done
Here is the code example:
1
2
3
4
5
6
7
8
9
10
11
| string Text = "456"; // string containing the number
int Result; //number which will contain the result
istringstream convert(Text); // stringstream used for the conversion constructed with the contents of 'Text'
// ie: the stream will start containing the characters of 'Text'
if ( !(convert >> Result) ) //give the value to 'Result' using the characters in the stream
Result = 0; //if that fails set 'Result' to 0
//'Result' now equal to 456
| |
This conversion is even easier to reduce to a single line:
1
2
3
| string Text = "456";
int Number;
if ( ! (istringstream(Text) >> Number) ) Number = 0;
| |
In the above code an object of istringstream gets constructed from 'Text'
istringstream(Text)
and its contents get read into the numeric variable
>> Number
.
If that operation fails
if ( !
, 'Number' is set to zero
Number = 0;
Locales and manipulators can be used as well as with any stream
More complex cases
A generic
stringstream
( which could be used both for input and for output ) can be useful in some more complex situations and in almost any situation you need to perform operations not provided by
string
Simple Sample Functions
Here are listed some functions to perform these conversion using stringstreams:
1
2
3
4
5
6
7
| template <typename T>
string NumberToString ( T Number )
{
ostringstream ss;
ss << Number;
return ss.str();
}
| |
Usage:
NumberToString ( Number );
1
2
3
4
5
6
7
| template <typename T>
T StringToNumber ( const string &Text )
{
istringstream ss(Text);
T result;
return ss >> result ? result : 0;
}
| |
Usage:
StringToNumber<Type> ( String );
Notice: In the code examples std:: was omitted to make the code simpler
Using the last functions, there is no way of detecting whether the conversion succeded or failed
C++11
C++11 introduced some standard library functions that can directly convert
basic types to std::string objects and vice-versa.
std::to_string Converts basic numeric types to strings.
The set of functions
stoi, stol, stoll convert to integral types, the functions
stof, stod, stold to floating-point values.
These functions are declared in declared in <string>.
Note that since these functions were added in the latest version of the C++ standard,
they may not be available unless your implementation is very recent.
1
2
3
4
5
| int number = 123;
string text = to_string(number);
text = "456"
number = stoi(number);
| |
C++ - Boost Library
Using stringstreams is the standard C++ way of doing these conversions but they usually need a few lines of code
Among the
Boost libraries there is
lexical_cast
which allows to perform the stringstream conversions through simple function call
To make this library working, just include the header, it doesn't need to be linked
1
2
3
4
5
6
7
8
| // Boost header needed:
#include <boost/lexical_cast.hpp>
// Number to string conversion:
Text = boost::lexical_cast<string>(Number);
// String to number conversion:
Number = boost::lexical_cast<Type>(Text);
| |
The above examples don't handle eventual conversion failures
When
boost::lexical_cast
fails, it throws
boost::bad_lexical_cast
( derived from
std::bad_cast
)
1
2
3
4
5
6
7
8
9
10
11
| try
{
Number = boost::lexical_cast<Type>(Text);
}
catch ( const boost::bad_lexical_cast &exc ) // conversion failed, exception thrown by lexical_cast and caught
{
Number = 0; // give 'Number' an arbitrary value ( in this case zero )
// if you don't give it any value, it would maintain the value it had before the conversion
// A string containing a description of the exception can be found in exc.what()
}
|
|
C - stdio
Number to String
In C there is no stream library, but the function
sprintf
can be used for conversion
It works in a similar way to
printf
but it will put the characters in a C string ( a character array ) instead of
stdout
Using this is not as easy as with streams as the format string changes depending on the type of the number which needs to be converted
Example:
1
2
3
4
5
| int Number = 123; // number to convert
char Result[16]; // string which will contain the number
sprintf ( Result, "%d", Number ); // %d makes the result be a decimal integer
| |
String to Number
As
printf
, also
scanf
has a related function which can read from a character array,
sscanf
1
2
3
4
5
| char Text[] = "456"; // string to be converted
int Result; // number which will contain the result
sscanf ( Text, "%d", &Result );
| |
If
sscanf
fails ( ie: the string is not a number ), the value of the variable passed remains unchanged, in that case the function should return zero as no argument were read successfully, if the string passed is so bad that nothing can be read from it, it would return
EOF
:
1
2
3
4
5
6
7
8
| char Text[] = "456"; // string to be converted
int Result; // number which will contain the result
int Succeeded = sscanf ( Text, "%d", &Result );
if ( !Succeeded || Succeeded == EOF ) // check if something went wrong during the conversion
Result = 0;
| |
C - stdlib
The
stdlib header contains some functions to convert text and numbers
Notice that
some of these functions are not standard! These functions are:
- For examples refer to the individual reference pages -
Writing your own function
Using already existing libraries is easier and better but, just to show how some of the above solutions work, here are some examples of how to write functions to convert text to numbers and numbers to text using only the core language, the following examples are from the book "
The C Programming Language"
Here is
itoa
( Integer TO Alphabet )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| /* itoa: convert n to characters in s */
void itoa(int n, char s[])
{
int i, sign;
if ((sign = n) < 0) /* record sign */
n = -n; /* make n positive */
i = 0;
do { /* generate digits in reverse order */
s[i++] = n % 10 + '0'; /* get next digit */
} while ((n /= 10) > 0); /* delete it */
if (sign < 0)
s[i++] = '-';
s[i] = '\0';
reverse(s);
}
| |
Here is the function
reverse
used in
itoa
:
1
2
3
4
5
6
7
8
9
10
11
12
| /* reverse: reverse string s in place */
void reverse(char s[])
{
int i, j;
char c;
for (i = 0, j = strlen(s)-1; i<j; i++, j--) {
c = s[i];
s[i] = s[j];
s[j] = c;
}
}
| |
reverse
uses the function
strlen
from the header cstring ( string.h in C )
This is easy to implement, here is an example:
1
2
3
4
5
6
7
8
| /* strlen: return length of s */
int strlen(char s[])
{
int i = 0;
while (s[i] != '\0')
++i;
return i;
}
| |
As you can see, is possible to create a ( bad ) conversion function with just some basic C
The same applies to the opposite conversion:
1
2
3
4
5
6
7
8
9
| /* atoi: convert s to integer */
int atoi(char s[])
{
int i, n;
n = 0;
for (i = 0; s[i] >= '0' && s[i] <= '9'; ++i)
n = 10 * n + (s[i] - '0');
return n;
}
| |
Of course these functions are bad for many reasons and should not be used in actual code
They just show the idea behind the conversion between an integer value and a character sequence