请问如何将utf-8转换成Unicode

abitworried2 2010-08-11 11:59:25
请问如何将utf-8转换成Unicode,这有一个方法,我没弄明白,使用后老死机或者重启:
int32 UTF8ToUnicode(const int8 *szSrc, int32 nSrcLen, wchar_t *strDest, int32 nDestLen)
{
int32 i=0;
int32 i_cur_output=0;

uint8 *pszSrc = (uint8 *)szSrc; /* cast to avoid signed/unsigned promomtion problems */
while ( i<nSrcLen )
{
if ( SIGMASK_3_1 <= pszSrc[i] ) /* 1st byte of 3 byte representation */
{
if ( i+2<nSrcLen && i_cur_output+1<nDestLen )
{
strDest[i_cur_output++] =(wchar_t)( (wchar_t)pszSrc[i] << 12 |
((wchar_t)pszSrc[i+1] & 0x3f) << 6 |
(wchar_t)pszSrc[i+2] & 0x3f);
i+=3;
}
else
{
return 0; /* ERROR_INSUFFICIENT_BUFFER */
}
}
else if ( SIGMASK_2_1 <= pszSrc[i] ) /* 1st byte of 2 byte representation */
{
if ( i+1<nSrcLen && i_cur_output+1<nDestLen )
{
strDest[i_cur_output++] = (wchar_t)(((wchar_t)pszSrc[i] & ~0xc0) << 6 |
((wchar_t)pszSrc[i+1] & ~0x80));
i+=2;
}
else
{
return 0; /* ERROR_INSUFFICIENT_BUFFER */
}
}
else /* Single byte representation */
{
if ( i<nSrcLen && i_cur_output+1<nDestLen )
{
strDest[i_cur_output++] = (wchar_t)pszSrc[i];
++i;
}
else
{
return 0; /* ERROR_INSUFFICIENT_BUFFER */
}
}
}
...全文
1696 1 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
jackyjkchen 2010-08-11
  • 打赏
  • 举报
回复
不靠谱,windows mobile下用MultibyteToWideChar这个api,跨平台就用iconv库吧,这东西不要自己写

2,853

社区成员

发帖
与我相关
我的任务
社区描述
本论坛以AI、WoS 、XR、IoT、Auto、生成式AI等核心板块组成,为开发者提供便捷及高效的学习和交流平台。 高通开发者专区主页:https://qualcomm.csdn.net/
人工智能物联网机器学习 技术论坛(原bbs) 北京·东城区
社区管理员
  • csdnsqst0050
  • chipseeker
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧