1. Define the macro in the header file;
# Define Unicode
# Define _ Unicode
2. Convert Char to wchar
Const Char * Pfilepathname = "C: \ AA. dll ";
Int Nlen = Strlen (pfilepathname) + 1;
Int Nwlen = Multibytetowidechar (cp_acp, 0, Pfilepathname, Nlen, Null, 0 );
Tchar Lpszfile [256];
Multibytetowidechar (cp_acp, 0, Pfilepathname, Nlen, Lpszfile, Nwlen );
3. Convert wchar to Char
Char * Pfilepathname;
Tchar Lpszfile [256];
_ Tcscpy (lpszfile, _ T ("C: \ AA. dll "));
Int Nlen = Wcslen (wstr) + 1;
Widechartomultibyte (cp_acp, 0, Lpszfile, Nlen, Pfilepathname, 2 * nlen, Null, Null );
4. char * And cstring Conversion
Cstring is a special C ++ object, which contains three values: A pointer to a data buffer, a valid string in the buffer (which is inaccessible and is a hidden area located under the cstring address), and a buffer length. The valid characters can be any number between 0 and the maximum buffer length minus 1 (because the string ends with a null character ). Characters and buffer length are cleverly hidden.
(1) convert char * To cstring
If char * is converted to cstring, you can use cstring: format in addition to direct value assignment. For example:
Char charray [] = "char test ";
Tchar * P = _ T ("char test"); (or lptstr P = _ T ("char test ");)
Cstring thestring = charray;
Thestring. Format (_ T ("% s"), charray );
Thestring = P;
(2) convert cstring to char *
If the cstring type is converted to the char * (lpstr) type, the following three methods are often used:
Method 1: use forced conversion. For example:
Cstring thestring (_ T ("char test "));
Lptstr lpsz = (lptstr) (lpctstr) thestring;
Method 2: Use strcpy. For example:
Cstring thestring (_ T ("char test "));
Lptstr lpsz = new tchar [thestring. getlength () + 1];
_ Tcscpy (lpsz, thestring );
It should be noted that the second parameter of strcpy (or _ tcscpy of the value to be moved) is const wchar_t * (UNICODE) or const char * (ANSI ), the system compiler will automatically convert it.
Method 3: Use cstring: getbuffer.
If you need to modify the content in cstring, there is a special method that can be used, that is, getbuffer, which is used to return a writable buffer pointer. If you only want to modify characters or truncate strings, for example:
Cstring S (_ T ("char test "));
Lptstr P = S. getbuffer ();
Lptstr dot = strchr (p ,''.'');
// Add the code using P here
If (P! = NULL)
* P = _ T ('\ 0 ');
S. releasebuffer (); // Release immediately after use, so that other cstring member functions can be used.
In the range between getbuffer and releasebuffer, you must not use any method of the buffer cstring object you want to operate on. Because the integrity of the cstring object is not guaranteed before releasebuffer is called.
5. Convert cstring to int
Converts a character to an integer. You can use atoi, _ atoi64, or atol.
CstringAaa="16";
IntInt_chage=Atoi (AAA);
GetInt_chage = 16
IntAtoi (constChar* Nptr );
LongAtol (constChar* Nptr );
LongLongAtoll (constChar* Nptr );
LongLongAtoq (constChar* Nptr );
6. Convert int to cstring
To convert a number to a cstring variable, you can use the format function of cstring. For example
CstringS;
IntI=64;
S. Format ("% d ",I)