GBK_TO_UTF8 程序实现方式

    网络上面有提到GBK向UTF8转换的实现方式,但是尝试之后不大准确。
    如下是自己根据GBK和UTF8的存储格式,实现GBK向UTF8转化。

    BYTE aGBK[3] = {0x00, 0x00, 0x00};
    int by1, by2, by3, iValue, iSub = 1, iLen = 0, iCount = 0;
    wchar_t aUnicode[2];
    BYTE *pString = NULL;
    iLen = sStr.Length();//sStr为函数输入参数,AnsiString
    pString = new BYTE [iLen * 3];
    while(iSub <= iLen)
    {
        if(0x80 > (iValue = (sStr[iSub] & 0xFF)))
        {
            pString[iCount++] = iValue;
        }
        else
        {
            aGBK[0] = (sStr[iSub++] & 0xFF);
            aGBK[1] = (sStr[iSub] & 0xFF);
            MultiByteToWideChar(CP_ACP, 0, aGBK, 3, aUnicode, 2);
            by1 = 0xE0;
            by2 = 0x80;
            by3 = 0x80;
            iValue = aUnicode[0];
            by3 |= (iValue & 0x0000003F);
            by2 |= ((iValue >> 6) & 0x0000003F);
            by1 |= ((iValue >> 12) & 0x0000000F);
            pString[iCount++] = by1;
            pString[iCount++] = by2;
            pString[iCount++] = by3;
        }
        iSub++;
    }


2010_10_18_16_59

你可能感兴趣的:(utf8)