Boost(4) Check lexical_cast and atoi,atof
Summary: This article, Boost color cast we can try to use lexical_cast information.
lexical_cast is, STL's also possible to convert from string to double or int to.
C language, atof, atoi to convert and use.
In C + +, std:: istringstream from the shift operator ( "") can be converted using.
One difference between these three, I would like to briefly examine.
Does, immediately, try using the sample source.
Sample source code you used here:
To ensure easy operation, I tried to create the following sample program.
In, I immediately look at the source code.
[sample.cpp]
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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 | #include <iostream> #include <string> #include <boost/algorithm/string.hpp> #include <boost/lexical_cast.hpp> using namespace std; using namespace boost; struct _test_value { const char *_char; const wchar_t *_wchar; }; static _test_value double_test[] = { { "hello world!123" , L"hello world!123" }, { "12345" , L"12345" }, { " 12345" , L" 12345" }, { "12345 " , L"12345 " }, { " 12345 " , L" 12345 " }, { "123.45" , L"123.45" }, { "6.543E+099" , L"6.543E+099" }, { "6.543e+099" , L"6.543e+099" }, { "1.2345e+999" , L"1.2345e+999" }, // stopper { NULL,NULL } }; int main(int argc, char *argv[]) { string str1; wstring str2; double db; int ni=0; cout << "== double convert" << endl; while(double_test[ni]._char!=NULL){ str1=double_test[ni]._char; str2=double_test[ni]._wchar; try{ db=lexical_cast<double>(str1); cout << str1 << ":"<< db << endl; } catch(bad_lexical_cast &e) { cout << str1 << "(error):" << e.what() << endl; } try{ db=lexical_cast<double>(str2); cout << str1 << ":"<< db << endl; } catch(bad_lexical_cast &e) { cout << str1 << "(error):" << e.what() << endl; } std::istringstream ss(str1); //std::wistringstream ss(str2); ss >> db; if(!(ss && (ss >> std::ws).eof())){ cout << "cast(error):"<< str1 << ":"<< db << endl; } else { cout << "cast:"<< str1 << ":"<< db << endl; } db=atof(str1.c_str()); cout << "atof:"<< str1 << ":"<< db << endl; cout << endl; ni++; } int nb; ni=0; cout << "== int convert" << endl; while(double_test[ni]._char!=NULL){ str1=double_test[ni]._char; str2=double_test[ni]._wchar; try{ nb=lexical_cast<int>(str1); cout << str1 << ":"<< nb << endl; } catch(bad_lexical_cast &e) { cout << str1 << "(error):" << e.what() << endl; } try{ nb=lexical_cast<int>(str2); cout << str1 << ":"<< nb << endl; } catch(bad_lexical_cast &e) { cout << str1 << "(error):" << e.what() << endl; } std::istringstream ss(str1); ss >> nb; if(!(ss && (ss >> std::ws).eof())){ cout << "cast(error):"<< str1 << ":"<< nb << endl; } else { cout << "cast:"<< str1 << ":"<< nb << endl; } nb=atoi(str1.c_str()); cout << "atoi:"<< str1 << ":"<< nb << endl; cout << endl; ni++; } string strss; cin >> strss ; // any key input!! --> exit. return 1; } |
This program, double_test test data (8bit string, UNCODE string) to convert sequentially, and outputs the result.
UNCODE For strings, lexical_cast is converted only.
Does, try a simple explanation.
Line 38 is to determine whether the end of the test data.
Line 44, Line 51, lexical_cast are carried out by the double conversion process.
If you fail to convert will be thrown bad_lexical_cast.
How to convert, specify as follows.
Return value, resulting in the conversion.
lexical_cast <class name you want to convert "(object to convert)
The first line 57 from line 64, C + + language has been converted using STL.
60 error checking is done on line, this is to judge whether the conversion process is carried out all the strings.
Line 66, C is the language I do the conversion.
atof, atoi does not check for errors.
以降は、intへの変換です。
Overview of the process is the same, so, we will omit the description.
In fact, try to run and displays the results following its execution.
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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | == double convert hello world!123(error):bad lexical cast: source type value could not be interpreted as target hello world!123(error):bad lexical cast: source type value could not be interpreted as target cast(error):hello world!123:-9.25596e+061 atof:hello world!123:0 12345:12345 12345:12345 cast:12345:12345 atof:12345:12345 12345(error):bad lexical cast: source type value could not be interpreted as target 12345(error):bad lexical cast: source type value could not be interpreted as target cast: 12345:12345 atof: 12345:12345 12345 (error):bad lexical cast: source type value could not be interpreted as target 12345 (error):bad lexical cast: source type value could not be interpreted as target cast:12345 :12345 atof:12345 :12345 12345 (error):bad lexical cast: source type value could not be interpreted as target 12345 (error):bad lexical cast: source type value could not be interpreted as target cast: 12345 :12345 atof: 12345 :12345 123.45:123.45 123.45:123.45 cast:123.45:123.45 atof:123.45:123.45 6.543E+099:6.543e+099 6.543E+099:6.543e+099 cast:6.543E+099:6.543e+099 atof:6.543E+099:6.543e+099 6.543e+099:6.543e+099 6.543e+099:6.543e+099 cast:6.543e+099:6.543e+099 atof:6.543e+099:6.543e+099 1.2345e+999(error):bad lexical cast: source type value could not be interpreted as target 1.2345e+999(error):bad lexical cast: source type value could not be interpreted as target cast(error):1.2345e+999:6.543e+099 atof:1.2345e+999:1.#INF == int convert hello world!123(error):bad lexical cast: source type value could not be interpreted as target hello world!123(error):bad lexical cast: source type value could not be interpreted as target cast(error):hello world!123:-858993460 atoi:hello world!123:0 12345:12345 12345:12345 cast:12345:12345 atoi:12345:12345 12345(error):bad lexical cast: source type value could not be interpreted as target 12345(error):bad lexical cast: source type value could not be interpreted as target cast: 12345:12345 atoi: 12345:12345 12345 (error):bad lexical cast: source type value could not be interpreted as target 12345 (error):bad lexical cast: source type value could not be interpreted as target cast:12345 :12345 atoi:12345 :12345 12345 (error):bad lexical cast: source type value could not be interpreted as target 12345 (error):bad lexical cast: source type value could not be interpreted as target cast: 12345 :12345 atoi: 12345 :12345 123.45(error):bad lexical cast: source type value could not be interpreted as target 123.45(error):bad lexical cast: source type value could not be interpreted as target cast(error):123.45:123 atoi:123.45:123 6.543E+099(error):bad lexical cast: source type value could not be interpreted as target 6.543E+099(error):bad lexical cast: source type value could not be interpreted as target cast(error):6.543E+099:6 atoi:6.543E+099:6 6.543e+099(error):bad lexical cast: source type value could not be interpreted as target 6.543e+099(error):bad lexical cast: source type value could not be interpreted as target cast(error):6.543e+099:6 atoi:6.543e+099:6 1.2345e+999(error):bad lexical cast: source type value could not be interpreted as target 1.2345e+999(error):bad lexical cast: source type value could not be interpreted as target cast(error):1.2345e+999:1 atoi:1.2345e+999:1 |
Table I summarizes the results easily.
| Boost lexical | std:: istringstream | atoi / atof | |
| In string | x | x | x (0) |
| Integer | o | o | o |
| Integer value (in the blank (before)) | x | o | o |
| Integer value (in space (after)) | x | o | o |
| Integer value (with a space (approx)) | x | o | o |
| Decimal | o | o (* 1) | o (* 1) |
| Small numbers (E representation) | o | o (* 1) | o (* 1) |
| Small numbers (e representation) | o | o (* 1) | o (* 1) |
| Small numbers (e overflow value representation) | x | x (* 1) | x (* 1) |
* 1: int conversion, retrieve only the integer part from the beginning.
In this way, lexical_cast check is very strict.
Alone there are around half-NG will be blank.
In contrast, C / C + + conversion in, you can convert even half blanks.
Also, lexical_cast is, int, double conversion as well as numbers, you can also convert to a string of numbers.
Is very convenient.
We recommend that color and tested.
You might also like:
Trackback URL
After Admin approves this comment, it will be shown.
Comments
Leave a Reply
