日期:2012-08-09  浏览次数:20429 次

ConVertUnicodeToUTF8(const WCHAR *wstrIn, BYTE *buff,int vLen)
{
    ASSERT(wstrIn);
    int wLen=wcslen(wstrIn);
    int i=0;
#define putchar(a)     buff[i++]=(BYTE)a;

    for(int j=0;(DWORD)j<wcslen(wstrIn);j++)
    {
        ASSERT(i<vLen);
        WCHAR 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);
        }
    }
}