Dear visitor who can read English
This page is a page translated automatically by Google AJAX Language API.
Please comment to this page if you could not understand. A PERSON will review this page .
I think that you will be able to understand this page later. Please give a little time to us.
Best regards,
This site is support & information site of WEB,and Software. This site might help you that create software or Web Site…perhaps?[:]
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.
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.
== 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.
In recent years, atoi, atof program that is intensive, and I think there is too much, atoi, atof instead, lexical_cast error has occurred and用Iyou intact, it may not work well .
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.
Leave a Reply