C++ int string 相互转换转换
2012-11-16 16:17
525 查看
Converting numbers to strings and strings to numbers
Score: 4.2/5 (131 votes)Converting numbers to text and vice versa is a common issue as it can be useful in many different situations and C++ 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++ - stringstreamsNumber to String
Custom Formatting
String to Number
Simple Sample Functions
C++ - boost library
C - stdio
C - stdlib
Writing your own function
C++ - stringstreams
TheC++ 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
stringstreamsyou can use the same syntax to convert the different numeric types.
To use
stringstreamsyou 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" |
1 2 | int Number = 123; string String = static_cast<ostringstream*>( &(ostringstream() << Number) )->str(); |
ostringstream() << NumberThen, 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 1112 | // 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
istringstreamwill 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 |
1 23 | string Text = "456"; int Number; if ( ! (istringstream(Text) >> Number) ) Number = 0; |
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 genericstringstream( 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 23 | template <typename T> string NumberToString ( T Number ) { ostringstream ss; ss << Number; return ss.str(); } |
NumberToString ( Number );
1 23 | template <typename T> T StringToNumber ( const string &Text ) { istringstream ss(Text); T result; return ss >> result ? result : 0; } |
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++ - Boost Library
Using stringstreams is the standard C++ way of doing these conversions but they usually need a few lines of codeAmong the Boost libraries there is
lexical_castwhich 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 23 | // 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); |
When
boost::lexical_castfails, 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 functionsprintfcan be used for conversion
It works in a similar way to
printfbut it will put the characters in a C string ( a character array ) instead of
stdoutUsing 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 23 | 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
Asprintf, also
scanfhas a related function which can read from a character array,
sscanf
1 23 | char Text[] = "456"; // string to be converted int Result; // number which will contain the result sscanf ( Text, "%d", &Result ); |
sscanffails ( 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 23 | 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
Thestdlib header contains some functions to convert text and numbers
Notice that some of these functions are not standard!These functions are:
itoa
atoi
atol
atof
strtol
strtoul
strtod
- 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 1112 | /* itoa: convert n to characters in s */ |
reverseused in
itoa:
1 2 3 4 5 6 7 8 9 10 1112 | /* 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; } } |
reverseuses the function
strlenfrom the header cstring ( string.h in C )
This is easy to implement, here is an example:
1 23 | /* strlen: return length of s */ int strlen(char s[]) { int i = 0; while (s[i] != '\0') ++i; return i; } |
The same applies to the opposite conversion:
1 23 | /* 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; } |
They just show the idea behind the conversion between an integer value and a character sequence
另外:C++ 0x包含了stoi和to_string函数
C++0x introduces
stoi(and variants for each numeric type) and
to_string, the counterparts of the C
atoiand
itoabut expressed in term of
std::string.
std::string s = std::to_string(42);
is therefore the shortest way I can think of.
Note: see [string.conversions] (21.5 in n3242)
参考:http://stackoverflow.com/questions/5590381/easiest-way-to-convert-int-to-string-in-c
相关文章推荐
- C++中int与string的相互转换
- (个人总结)C++中int类型与String类型的相互转换
- C++int类型与string类型相互转换
- C++中 char*, string, CString, int, double之间相互的转换
- C++中的int和string的相互转换
- C++中int与string的相互转换
- C++中char*,string,int类型的相互转换
- C++中int、string、const char*、CString之间的相互转换
- C++ int 和 string 之间的相互转换
- C++ int与string相互转换
- C++ int和string相互转换
- c++中 int 和 string 的相互转换
- C++中string和Int的相互转换
- C++ int类型和string类型的相互转换
- (c++) int 转 string,char*,const char*和string的相互转换
- (c++) int 转 string,char*,const char*和string的相互转换
- C++中string和int相互转换
- C++ - string类型与int类型相互转换
- c++中int,string,char类型的相互转换
- [C++] string与int, float, double相互转换