`
touchinsert
  • 浏览: 1355953 次
  • 性别: Icon_minigender_1
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

ConVertUnicodeToUTF8

 
阅读更多

网上有很多,改一改,自已用得顺手一点儿

#include <vector>


/*!
@brief unicode to utf8
@param wstrIn 输入的unicode string缓存冲
@param wLen wstrIn的大小
@param utf8_string 输出的utf8 string
@return
********************************************************************/
void ConVertUnicodeToUTF8(const unsigned short *wstrIn,int wLen, std::vector<unsigned char>& utf8_string)
{
if(NULL == wstrIn || 0 == wLen)
return;

int i=0;

#define putchar(a) utf8_string.push_back(a);

for(int j=0;(unsigned long)j<wLen;j++)
{
//ASSERT(i<vLen);
unsigned short c=wstrIn[j];
if (c < 0x80)
{
putchar (c);
}
else if (c < 0x800)
{
putchar (0xC0 | c>>6);
putchar (0x80 | c & 0x3F);
}
else if (c < 0x10000)
{
putchar (0xE0 | c>>12);
putchar (0x80 | c>>6 & 0x3F);
putchar (0x80 | c & 0x3F);
}
else if (c < 0x200000)
{
putchar (0xF0 | c>>18);
putchar (0x80 | c>>12 & 0x3F);
putchar (0x80 | c>>6 & 0x3F);
putchar (0x80 | c & 0x3F);
}
}
#undef putchar
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics