int unicodeLen = WideCharToMultiByte(CP_ACP, NULL, str, -1, NULL, 0, NULL, NULL);
BYTE* UTF8Char = new BYTE[unicodeLen + 1];
::WideCharToMultiByte(CP_UTF8, NULL, str, -1, (char*)UTF8Char, unicodeLen,  NULL, NULL);
我是按照上面的方式进行了转换,但是转换出来的中文还是有问题!中文乱码!UnicodeUTF-8

解决方案 »

  1.   

    1) 将一个字符的Unicode(UCS-2和UCS-4)编码转换成UTF-8编码.// #c---
    /*****************************************************************************
     * 将一个字符的Unicode(UCS-2和UCS-4)编码转换成UTF-8编码.
     *
     * 参数:
     *    unic     字符的Unicode编码值
     *    pOutput  指向输出的用于存储UTF8编码值的缓冲区的指针
     *    outsize  pOutput缓冲的大小
     *
     * 返回值:
     *    返回转换后的字符的UTF8编码所占的字节数, 如果出错则返回 0 .
     *
     * 注意:
     *     1. UTF8没有字节序问题, 但是Unicode有字节序要求;
     *        字节序分为大端(Big Endian)和小端(Little Endian)两种;
     *        在Intel处理器中采用小端法表示, 在此采用小端法表示. (低地址存低位)
     *     2. 请保证 pOutput 缓冲区有最少有 6 字节的空间大小!
     ****************************************************************************/
    int enc_unicode_to_utf8_one(unsigned long unic, unsigned char *pOutput,
            int outSize)
    {
        assert(pOutput != NULL);
        assert(outSize >= 6);    if ( unic <= 0x0000007F )
        {
            // * U-00000000 - U-0000007F:  0xxxxxxx
            *pOutput     = (unic & 0x7F);
            return 1;
        }
        else if ( unic >= 0x00000080 && unic <= 0x000007FF )
        {
            // * U-00000080 - U-000007FF:  110xxxxx 10xxxxxx
            *(pOutput+1) = (unic & 0x3F) | 0x80;
            *pOutput     = ((unic >> 6) & 0x1F) | 0xC0;
            return 2;
        }
        else if ( unic >= 0x00000800 && unic <= 0x0000FFFF )
        {
            // * U-00000800 - U-0000FFFF:  1110xxxx 10xxxxxx 10xxxxxx
            *(pOutput+2) = (unic & 0x3F) | 0x80;
            *(pOutput+1) = ((unic >>  6) & 0x3F) | 0x80;
            *pOutput     = ((unic >> 12) & 0x0F) | 0xE0;
            return 3;
        }
        else if ( unic >= 0x00010000 && unic <= 0x001FFFFF )
        {
            // * U-00010000 - U-001FFFFF:  11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
            *(pOutput+3) = (unic & 0x3F) | 0x80;
            *(pOutput+2) = ((unic >>  6) & 0x3F) | 0x80;
            *(pOutput+1) = ((unic >> 12) & 0x3F) | 0x80;
            *pOutput     = ((unic >> 18) & 0x07) | 0xF0;
            return 4;
        }
        else if ( unic >= 0x00200000 && unic <= 0x03FFFFFF )
        {
            // * U-00200000 - U-03FFFFFF:  111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
            *(pOutput+4) = (unic & 0x3F) | 0x80;
            *(pOutput+3) = ((unic >>  6) & 0x3F) | 0x80;
            *(pOutput+2) = ((unic >> 12) & 0x3F) | 0x80;
            *(pOutput+1) = ((unic >> 18) & 0x3F) | 0x80;
            *pOutput     = ((unic >> 24) & 0x03) | 0xF8;
            return 5;
        }
        else if ( unic >= 0x04000000 && unic <= 0x7FFFFFFF )
        {
            // * U-04000000 - U-7FFFFFFF:  1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
            *(pOutput+5) = (unic & 0x3F) | 0x80;
            *(pOutput+4) = ((unic >>  6) & 0x3F) | 0x80;
            *(pOutput+3) = ((unic >> 12) & 0x3F) | 0x80;
            *(pOutput+2) = ((unic >> 18) & 0x3F) | 0x80;
            *(pOutput+1) = ((unic >> 24) & 0x3F) | 0x80;
            *pOutput     = ((unic >> 30) & 0x01) | 0xFC;
            return 6;
        }    return 0;
    }
    // #c---end
      

  2.   

    UTF8的中文本来就是乱码
    Unicode转UTF-8很简单
    std::string Unicode2UTF8(const TCHAR* p_input)
    {
    std::string s_result;
    CT2A input(p_input, CP_UTF8);
    s_result = input;
    return s_result;
    }
      

  3.   

    刚刚试了一下,因为我要在PC上生成一个sqlite的数据库,然后在Android上用, 用你这个方法部分字符串在Android上可以识别出来,像"张三""李四"之类的都可以!但是像"张""张3"之类的,在下面看到还都是乱码!
      

  4.   

    Unicode -> UTF8
    WideCharToMultiByte(CP_UTF8, ....);
      

  5.   

    这个试过了 ,不行!转换的UTF-8在Android上读出来是错的!
      

  6.   

    int unicodeLen = WideCharToMultiByte(CP_UTF8, NULL, str, -1, NULL, 0, NULL, NULL);
      

  7.   

    WideCharToMultiByte这个转化不行,用sqlitespy看到的还是乱码!
      

  8.   

    int unicodeLen = WideCharToMultiByte(CP_ACP, NULL, str, wcslen(str), NULL, 0, NULL, NULL);
      

  9.   

    不知道处理不处理NULL结尾相关不.  不相关可以忽略16楼