char和wchar_t互相转换

wchar_t -> char

void WideCharToChar(const LPCWSTR, lpWideCharStr, const int cchWideChar,
 LPSTR   lpMultiByteStr)
{
  WideCharToMultiByte(CP_ACP, 0, lpWideCharStr, cchWideChar, lpMultiByteStr,
   2*cchWideChar, NULL, NULL);
  return ;
}
//调用
wchar_t wch[] = _T("123");
int iwchLeng = wcslen(wch);
char *ch = new char[iwchLeng+1];
memset(ch, '\0', iwchLeng+1);
WideCharToChar(wch, ch, iwchLeng);

char -> wchar_t

void CharToWideChar(LPCSTR lpMultiByteStr, int cbMultiByte, LPWSTR
lpWideCharStr, int cchWideChar)
{
  MultiByteToWideChar(CP_ACP, 0, lpMultiByteStr, cbMultiByte, lpWideCharStr, cchWideChar); 
  return ;
}
//调用
char ch[] = "abc'";
int ichLength = strlen(ch)+1;
int iwchLength = MultiByteToWideChar(CP_ACP, 0, ch, ichLength, NULL, 0);
wchar_t *wch = new wchar_t[iwchLength];
memset(wch, '\0', iwchLength);
CharToWideChar(ch, ichLength, wch, iwchLength);

你可能感兴趣的:(C++程序设计语言)