Int char * string float cstring common Conversions

Source: Internet
Author: User

 

Int to char */Char []
Method:
Int num = 0;
Char temp [64];
Sprintf (temp, "% d", num );
Or
Int number = 123456;
Char string [25];
ITOA (number, String, 10); // 10 converts the base number of a number (in hexadecimal notation). 10 is

Converts a number in decimal format. It can also be 2, 8, 16, and so on.

 

Char * to string
Method:
Char temp [64];
String str1 (temp );
You can only initialize or assign values. It is best to use assign () instead of initialization ().
String & assign (const char * s); values are assigned by string s of the C type.

Or
Char temp [64];
String str1;
Str1 = temp;

 

Int to string
Use int to char */Char [] and char * to string.

String to char *
Method:
Char C [20];
String S = "1234 ";
Strcpy (C, S. c_str ());
The following method is not recommended.
String STR;
Char * temp;
Temp = filename. c_str ()
// C finally points to the content of spam, because the S object is destructed and its content is processed

 

 

String to int
Method:

String source = "1412 ";
Int result = 0;

// Use convert. toint32 (string value );
Result = convert. toint32 (source );

// Use int32.parse (string value );
Result = int32.parse (source );

// Use int32.tryparse (string S, out int result );
Int32.tryparse (source, out result );
The three differences are as follows:

Http://blog.csdn.net/clever_yang/archive/2009/03/20/400800

5. aspx

① Cstring to float
Cstring strtemp = "11.22 ";
Float m_drundistance = atof (strtemp); // converts strtemp to float
Error message:
. Cpp (815): Error c2664: 'atof ': cannot convert parameter 1 from 'cstring' to 'const char *'
Cause: Double atof (const char * nptr );
Solution Analysis: Convert cstring to char *: Use the getbuffer method of the cstring object, so m_drundistance = atof (strtemp. getbuffer (0); an error occurs:
CPP (815): Error c2664: 'atof ': cannot convert parameter 1 from 'wchar _ T *' to 'const char *'
It indicates that the above method does not work, so use
Method 1: forced conversion of lpctstr (though not recommended)
M_drundistance = atof (char *) lpctstr (strtemp ));
The test is successful and no data is lost after the test.
Method 2: Use the uses_conversion Macro. However, this macro cannot be used in a Large Loop body or a large function, because it can be released only when the function is completed, so there is a stack overflow problem.
Uses_conversion
M_drundistance = atof (T2A (strtemp ));
Method 3: Convert the cstring type to char * and then return it to atof. (This method is feasible)
Char STR [1024];
Cstring str2;
Widechartomultibyte (cp_acp, 0, str2,-1, STR, 1024, null, null );
Float FF = atof (STR );

 

② Cstring to int
// Convert the id from cstring to int
Cstring tempfield1;
Int ruleid = _ tstoi (tempfield1 );
// Convert the id from Char to int
Char tempfield1 [10];
Ruleid = atoi (tempfield1 );

③ Cstring to char *
Method 1. widechartomultibyte () function
Cstring str2;
Char STR [1024];
Widechartomultibyte (cp_acp, 0, str2,-1, STR, 1024, null, null );
Method 2: T2A ()
Uses_conversion
Char * STR = T2A (strtemp );
Method 3: forced conversion
Char * STR = (char *) lpctstr (strtemp );

④ Convert char *, Int, float, and Other types to cstring
This type of conversion is easier. You can convert it using formats such as format or sprintf.
For example, int K = 10;
Cstring strtype;
Strtype. Format (_ T ("% d", delimiter? K ));
Char * Ch;
Cstring STR;
Ch = (lpstr) (lpctstr) STR;
STR = "Good! ";
Sprintf (CH, "% s", (lptstr) (lpctstr) Str );

 

4. Security
Cstring> string> char *;
5. Flexibility
Cstring> string> char *;
6. Portability
Char * = string> cstring;

 

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.