请问如何将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库吧,这东西不要自己写