随笔-9  评论-6  文章-5  trackbacks-0

开始尝试了以下两个方法
//*************************************************************//
//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 阅读(710) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理