開始嘗試了以下兩個方法
//*************************************************************//
//wchar_t 轉換 為char
//*************************************************************//
bool wstr2cstr(wchar_t *orig,char * target)
{
?size_t origsize = wcslen(orig) + 1;
?size_t convertedChars = 0;
?wcstombs_s(&convertedChars, target, origsize, orig, _TRUNCATE);
?return true;
}
//*************************************************************//
//char 轉換 為wchar_t
//*************************************************************//
bool cstr2wstr(char *orig,wchar_t * target)
{
?size_t origsize = strlen(orig) + 1;
?size_t convertedChars = 0;
?mbstowcs_s(&convertedChars, target, origsize, orig, _TRUNCATE);
?return true;
}
發現wstr2cstr沒有反應,cstr2wstr正常。想不通。
后來換成
//*************************************************************//
// 將 寬字節wchar_t* 轉換 單字節char*
//*************************************************************//
void UnicodeToAnsi( const wchar_t* szStr, char* pResult)
{
?int nLen = WideCharToMultiByte( CP_ACP, 0, szStr, -1, NULL, 0, NULL, NULL );
?if (nLen == 0)
?{
??? return;
?}
?WideCharToMultiByte( CP_ACP, 0, szStr, -1, pResult, nLen, NULL, NULL );
}
//*************************************************************//
//將 單字節char* 轉換為 寬字節 wchar*
//*************************************************************//
void AnsiToUnicode( const char* szStr , wchar_t* pResult )
{
?int nLen = MultiByteToWideChar( CP_ACP, MB_PRECOMPOSED, szStr, -1, NULL, 0 );
?if (nLen == 0)
?{
??? return;
?}
?MultiByteToWideChar( CP_ACP, MB_PRECOMPOSED, szStr, -1, pResult, nLen );
}
就好了
posted on 2008-12-19 16:40
BirdsHover 閱讀(736)
評論(0) 編輯 收藏 引用