mbstowcs and MultiByteToWideChar
mbstowcs()是MultiByteToWideChar()的簡化版,除了參數少了,本來以為參數少了一些,並不會影響使用,今天卻發現了它的一個小問題,在中文字符的轉換上,前者似乎無法對中文字符進行處理,轉換後顯示成亂碼,而後者則不會。
代碼:
WCHAR strPath[MAX_PATH];
CHAR buf[] = 「i am 中國人」;
// 簡化版對中文字符的轉換不好
// mbstowcs( strPath, buf, MAX_PATH );
// 還是這個比較好
MultiByteToWideChar(CP_ACP, 0, buf, strlen(buf), strPath, MAX_PATH );
///////////////////////////////////////////////////////////////////////
void CharArrayToWCAHRArray(const char* pSrc,int nSrcLen,WCHAR *szpDst)
{
int nwLen = MultiByteToWideChar(CP_ACP, 0, pSrc,
nSrcLen, NULL, 0);
MultiByteToWideChar(CP_ACP, 0, pSrc, nSrcLen, szpDst,
nwLen+1);
}
////////////////////////////////////實例 ///////////////////////////////////////////
#include "stdafx.h"
#include <windows.h>
#include <commctrl.h>
void CharArrayToWCAHRArray(const char* pSrc,int nSrcLen,WCHAR *szpDst)
{
int nwLen = MultiByteToWideChar(CP_ACP, 0, pSrc,
nSrcLen, NULL, 0);
MultiByteToWideChar(CP_ACP, 0, pSrc, nSrcLen, szpDst,
nwLen+1);
}
int _tmain(int argc, _TCHAR* argv[])
{
WCHAR strpath[MAX_PATH];
CHAR buf[] = "i am 中國人";
//mbstowcs(strpath,buf,MAX_PATH);
CharArrayToWCAHRArray(buf,strlen(buf)+1,strpath);
//RETAILMSG(1,(TEXT("strpath==%s")strpath));
RETAILMSG(1,(L"buf==%d\r\n",strlen(buf)));
RETAILMSG(1,(L"strpath==%d\r\n",wcslen(strpath)));
RETAILMSG(1,(L"strpath==%s\r\n",strpath));
return 0;
}
0 意見 (+add yours?)
張貼留言