Common Data Conversion in VC development database applications

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.