Unicode conversion ascii code, wchar * To char *, unicodewchar
For the ascii code, char is actually the first bytecode of the unicode code wchar,
For example, wchar [20] = "qqqq"; in the memory, the code is actually char's 'q'' \ 0', so if we write the unicode code to convert it to an ascii char, you only need to take the first byte, and I have written a function for conversion from wchar to char as follows. The code is simple and the memory leakage test method is added.
# Include <stdio. h> # ifdef _ DEBUG # define DEBUG_CLIENTBLOCK new (_ CLIENT_BLOCK, _ FILE __, _ LINE __) # else # define DEBUG_CLIENTBLOCK # endif # define _ CRTDBG_MAP_ALLOC # include <stdlib. h> # include <crtdbg. h> # include <tchar. h> # include <Windows. h> # ifdef _ DEBUG # define new DEBUG_CLIENTBLOCK # endifchar * UnicodeToMultibyte (WCHAR * wStr) {if (NULL! = WStr) {int nLen = wcslen (wStr); char * pStr = new char [nLen + 1]; int I = 0; for (; I <nLen; ++ I) {(pStr) [I] = (char) * (wStr + I);} (pStr) [I] = '\ 0'; printf ("% s \ n ", pStr); return pStr;} return NULL;} int main () {_ CrtSetDbgFlag (_ CRTDBG_ALLOC_MEM_DF | _ CRTDBG_LEAK_CHECK_DF);/* char * str = new char [100]; */WCHAR wStr [250] = L "qqqqqqqqqqqqq"; char szTemp [250] = _ T ("wwwwwwwwwwwww"); char * pTemp = UnicodeToMultibyte (wStr); strcpy (szTemp, pTemp); delete pTemp; // note that printf ("% s \ n", szTemp); return 0 ;}
Of course, you can also use the Conversion Function of windows. I will not explain it in detail on this msdn.
char* ConvertLPWSTRToLPSTR (LPWSTR lpwszStrIn) { LPSTR pszOut = NULL; if (lpwszStrIn != NULL) { int nInputStrLen = wcslen (lpwszStrIn); // Double NULL Termination int nOutputStrLen = WideCharToMultiByte (CP_ACP, 0, lpwszStrIn, nInputStrLen, NULL, 0, 0, 0) + 2; pszOut = new char [nOutputStrLen]; if (pszOut) { memset (pszOut, 0x00, nOutputStrLen); WideCharToMultiByte(CP_ACP, 0, lpwszStrIn, nInputStrLen, pszOut, nOutputStrLen, 0, 0); } } return pszOut; }