专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > Brew

请教怎么将utf-8转换成Unicode

发布时间:2010-05-30 20:18:25 文章来源:www.iduyao.cn 采编人员:星星草
请问如何将utf-8转换成Unicode
请问如何将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 */
  }
  }
  }


------解决方案--------------------
不靠谱,windows mobile下用MultibyteToWideChar这个api,跨平台就用iconv库吧,这东西不要自己写
友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: