I. Conversion of string types in VC
I always think that the types in VC are complicated and confusing. With Unicode, the C and C ++ types are converted to each other so differently. You can use short, unsigned short, and int to save more than one char. There are multiple types of strings, and there are more than one other type. In addition, C ++ does not have a formal string type. Therefore, many types of types with the same meaning but different types, or the same type and different writing types are usually encountered.
How to convert between them is also a problem that cannot be avoided in learning VC.
Which of the following types of commonly used string storage?
Cstring, tchar *, tchar [], char *, lpcstr, lpctstr, lpbyte
The above are more useful in VC. How to convert between them:
Here, we regard cstring as the intermediate value of a conversion. Any type is consistent with it.
1.Lpcstr success →Cstring
In VC, The lpcstr represents char * bytes, but it cannot be converted using char. It can be directly exchanged with cstring, just as belowCodeIn this way:
Cstring strhello; strhello ="Hello World"; Lpcstr lpstr = (lpcstr) strhello; cstring str2; str2 = (cstring) lpstr;
In addition, the cstring class has a member function format, which can be used to convert the type to the cstring type. The preceding lpstr conversion to the cstring type can be expressed as follows:
Str2.format ("% S", Lpstr );
2.LpctstrElse →Cstring
The conversion methods of the two types are the same. The value of the lpctstr parameter is unsigned short *. In this way, it supports the Unicode character type, while the value of the lpcstr parameter only supports the ANSI character type. Generally, we can use lpctstr to replace it.
3.Tchar * (char *)Else →Cstring
Tchar indicates unsigned short in VC and supports Unicode character sets. The conversion between two types can be as follows:
Tchar * mychars = strhello. getbuffer (strhello. getlength (); str2 = (cstring) mychars;// Or str2.format ("% s", mychars );
Note: conversion from cstring to tchar * cannot be performed using the following code:
Mychars = (lpctstr) strhello;// Error, can not convert from 'const char * 'to 'Char *'
It cannot be converted like this:
Mychars = (lptstr) strhello;// Cstring cannot be converted to char *
But it can be like this:
Mychars = (lptstr) (lpctstr) strhello;// Convert strhello to const char * and then to char *
4.LpbyteElse →Cstring
Lpbyte is of the unsigned char * type, and the conversion between them is as follows:
Lpbyte = (lpbyte) strhello. getbuffer (strhello. getlength ());
// Getbuffer returns the lptstr type.
// You can also convert it like this:
5.Tchar []Else →Cstring
The conversions between them are different because arrays are involved. Therefore, use the strcpy function. The strcpy function prototype is as follows:
Lptstr strcpy (lptstr psz1, lpctstr)
The conversion between them is as follows:
Char strchar [200] strcpy (strchar, (lpctstr) strhello); cstring str2 = (cstring) strchar;// Or use the format Function
It can be seen that cstring conversion to other types must be converted to the left-side query string or the left-side query string type (the left-side query string and the left-side query string represent const char * and const short *). Then, it is converted to another type (including unsigned char * and char *) through the lpcstr or lpctstr. Therefore, cstring must be converted to another type, first, check whether this type is const char * or const short *. If yes, you can directly convert it. If not, first convert it to the lpcstr or lpctstr type, and then forcibly convert them to the corresponding string type.
To convert any type to cstring, you must add (cstring) to the front to force the conversion, or use the format function of the cstring class.
Ii. Conversion of other common types in VC
Many functions use <stdlib. h> and <wchar. h>. If they are used, the header file must be included in the source file.
1.String to other types
double atof ( char * string); double wtof ( const wchar_t * string); int atoi ( const char * string); _ int64 _ atoi64 ( const char * string) int wtoi ( const wchar_t * string ); int64 wtoi64 ( const wchar_t * string); long atol ( const char * string); long wtol ( const wchar_t * string);
Example:
char * s = "-2309.12e-15 "; double X = atof (s ); // out:-2309120e-012 S = "-9885 pigs "; int I = atoi (s ); // out:-9885
2. convert other types to the struct type:
char * ITOA ( int value, char * string, int Radix ); char * i64toa (int64 value, char * string, int Radix); char * ui64toa ( unsigned int64 value, char * string, int Radix); wchar_t * itow ( int value, wchar_t string, int Radix); char * _ ltoa ( long value, char * string, int Radix);
Example:
int I = 100; ITOA (I, temp, 10); // decimal ITOA (I, temp, 2); // binary cstring STR; str. fomat ( "% d" , I); // integer to cstring Str. format ( "% F" , f); // float to cstring