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
}