Streaming with C++:
# include & ltsstream & gt
# Include & lt string & gt
String num2str (double I)
...{
stringstream ss
Ss<& lt me;
Returns ss.str ();
}
String to number:
Int str2num (string s)
...{
int num
string stream ss(s);
ss & gt& gtnum
Quantity returned;
}
The above method is very simple, but the disadvantage is that the conversion speed is slow when dealing with a large number of data. ..
Sprintf and sscanf in c library are relatively faster.
You can use the sprintf function to convert numbers by outputting them to a character buffer. ...
For example:
Knowing the number of seconds from 0 o'clock, calculate the string "H:M:S", where h is the hour, m = minutes and s = seconds.
int H,M,S;
String time _ str
H = seconds/3600;
M= (seconds% 3600)/60;
S= (seconds% 3600)% 60;
char ctime[ 10];
sprintf(ctime," %d:%d:%d ",H,M,S); //Convert an integer into a string
Time _ str = ctime// result
The corresponding to sprintf is the sscanf function, which can convert strings into numbers.
char str[]= " 15.455 ";
int I;
Floating point FP;
sscanf( str," %d ",& ampI); //Convert a string into an integer i = 15.
sscanf( str," %f ",& ampFP); //Convert the string into floating point fp = 15.455000.
printf( "Integer: = %d ",I+ 1);
printf( "Real: = %f ",FP+ 1);
Returns 0;
The output is as follows:
Integer: = 16
Real number: = 16.455000
/2009/09 17/ 15494