Writing database programs with VC will inevitably encounter the conversion problem between the _ bstr_t, cstring, and ctime types, which is a headache. This morning, we finally solved the problem of cstring and ctime type conversion.
// Ctime --> cstring
Ctime T;
T = ctime: getcurrenttime ();
Cstring Sur;
Sur. Format ("% s", T. Format ("% Y-% m-% d "));
MessageBox (SUR );
// Long --> cstring
Long;
Cstring B;
B. Format ("% lD", );
// Double ---> cstring
Double;
Cstring B;
B. Format ("% F", );
// Cstring --> ctime
Cstring Sur;
Sur = "2006-08-09 ";
Coledatetime time1;
Time1.parsedatetime (SUR );
Systemtime receivime;
Varianttimetosystemtime (time1, & systime );
Ctime TM (systime );
M_time = TM;
Updatedata (false );
Conversion between cstring, Int, and float.
1. Int <-> cstring
1) int-> cstring
Int n = 1;
Cstring STR;
Str. Format ("% d", N );
2) cstring-> int
Cstring STR = "1 ";
Int n = atoi (Str. getbuffer (0 ));
2. char * And cstring
1) char *-> cstring
Char SZ [128];
Cstring STR;
Str. Format ("% s", SZ );
2) cstring-> char *
Cstring STR;
Int nlength = Str. getlength ();
Char * SZ = new char [nlength];
SZ = Str. getbuffer (0 );
3. Float <-> cstring1) float-> cstring
Float F = 0.0;
Cstring STR;
Str. Format ("% F", F );
2) cstring-> float
Cstring STR = "0.0 ";
Float F = atof (Str. getbuffer (0 ));
The first parameter of the format function is the type of the number to be converted.