Unicode conversion ascii code, wchar * To char *, unicodewchar

Source: Internet
Author: User

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;  }


 

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.