1, the conversion between string numbers
(1) string--> char *
string str ("OK");
char * p = str.c_str ();
(2) char *-->string
char *p = "OK";
String str (p);
(3) char *-->cstring
char *p = "OK";
CString m_str (p);
or
CString m_str;
M_str.format ("%s", p);
(4) CString--> char *
CString str ("OK");
char * p = str. GetBuffer (0);
...
Str. ReleaseBuffer ();
(5) String--> CString
cstring.format ("%s", String.c_str ());
(6) CString--> String
string s (Cstring.getbuffer (0));
GetBuffer () must be releasebuffer (), otherwise there is no free buffer space occupied, CString objects can not dynamically grow.
(7) double/float->cstring
double data;
Cstring.format ("%.2f", data); Retains 2 decimal places
(8) cstring->double
CString s= "123.12";
Double D=atof (s);
(9) String->double
double D=atof (S.c_str ());
2. Digital spin string: Using the sprintf () function
Char str[10];
int a=1234321;
sprintf (str, "%d", a);
--------------------
Char str[10];
Double a=123.321;
sprintf (str, "%.3LF", a);
--------------------
Char str[10];
int a=175;
sprintf (str, "%x", a);//10 is converted to 16, if the output uppercase letter is sprintf (str,%x, a)
--------------------
char *itoa (int Value, char* string, int radix);
You can also spin a number to a string, but itoa () is a platform-dependent (not a standard), so this function is not recommended here.
3. String number: Using the sscanf () function
Char str[]= "1234321";
int A;
SSCANF (str, "%d", &a);
.............
Char str[]= "123.321";
Double A;
SSCANF (str, "%LF", &a);
.............
Char str[]= "AF";
int A;
SSCANF (str, "%x", &a); 16-in-system conversion to 10-system
Alternatively, you can use Atoi (), Atol (), Atof ().
4, the use of StringStream class
Write a string with the Ostringstream object, similar to sprintf ()
Ostringstream S1;
int i =;
S1 << "Hello" << i << Endl;
String s2 = S1.str ();
cout << S2;
Read a string with the Istringstream object, similar to SSCANF ()
Istringstream stream1;
string string1 = "n";
STREAM1.STR (string1);
int i;
Stream1 >> i;
cout << i << Endl; Displays 25
The above is a small series of C + + in the number and string between the conversion method (recommended) of the whole content, I hope to help you, a lot of support cloud Habitat Community ~