String: How to convert a string as a digital type?

zhaozj2021-02-16  52

String: How to convert a string as a digital type?

When the string is converted into a numeric type, there is something that you are not allowed: Conversion may fail because of the string you are converting, may not contain a valid digital representation failure

For example, if you want to convert "Hello" into a number, the conversion will fail

Old C method (disapproval)

Many people use other functions in ATOI (), ATOF () and this "family". They are convenient to apply, but there is an important disadvantage: returning 0 when the conversion failure and the conversion string "0", which makes consistency Error check becomes almost impossible. For integrity, we give the small code:

Code: ------------------------------------------------ -------------------------------- Const char * STR_INT = "777"; const char * str_float = "333.3"; INT i = ATOI (STR_INT); float f = atof (str_float); ---------------------------------- ----------------------------------------------

A better way:

More complex, more inconsistent methods use SSCANF ()

Code: ------------------------------------------------ -------------------------------- Const char * STR_INT = "777"; const char * str_float = "333.3"; INT i; float f; if (eof == sscanf (str_int, "% d", & i)) {// error} if (eof == sscanf (Str_float, "% f", & f)) {// error} -------------------------------------------------- ------------------------------

Since sscanf () Takes a constly use a cstring with it: because sscanf () uses const char * as a parameter, you can use the cstring as a parameter directly:

Code: ------------------------------------------------ -------------------------------- CString STR_INT ("777"); if (EOF == SSCANF (STR_INT, " % D ", & i)) {// error} -------------------------------------- ------------------------------------------

Carefully format descriptors (as "% D" in this example). SSCANF () There is no way to check the format descriptor matches the type of transfer variable. If you do not match you will get unpredictable results. Also note that SSCANF () can extract one or more values ​​from the string once. For more information, please check the MSDN.

C method

The following example shows a template function that uses standard C classes to complete this task

Code: ------------------------------------------------ -------------------------------- # include #include #include template bool from_string (t & t, const st: string & s, std :: os_base & (* f) (std :: ostringstream ISS (STD :: istringstream ISS (S); Return! (ISS >> F >> t) .fail ();

INT main () {Int i; float f; // from_string () The third parameter should be one of the following // one of std :: hex, std :: dec, std :: 动 c (from_string (i, std :: string ("ff"), std :: hex) {std :: cout << i << std :: end1;} else {std :: cout << "from_string failed" << Std :: endl;} if (from_string (f, std :: string ("123.456"), std :: dec)) {std :: cout << f << std :: endl;} else {std :: Cout << "from_string failed" << std :: endl;} return 0;

/ * Output: 255123.456 * / ------------------------------------------------------------------------------------------- -------------------------------------

This method is not only very chic, but also type is safe because the compiler will select the appropriate std :: istringstream :: Operator >> () based on the type of operand when compiling.

转载请注明原文地址:https://www.9cbs.com/read-24457.html

New Post(0)