GBK与UTF8 编码转化(C++)

引用:http://www.vimer.cn/2009/10/gbk%E4%B8%8Eutf8-%E7%BC%96%E7%A0%81%E8%BD%AC%E5%8C%96%EF%BC%88c%EF%BC%89.html

//GBK 2 UTF8
int API_Gbk2Utf8(const char *szSource, string &strDest)
{
    char szUniString[strlen(szSource)*2];
    int iLen = string_gbk2unicode(szSource, szUniString, strlen(szSource));
    unsigned char pTemp[4= {0};
    unsigned short iTemp;
    char *pUTFString = (char *)malloc(sizeof(szUniString) *2+1);
    int pos=0;
    for (int i=0; i<iLen; i++)
    {
        iTemp =(((unsigned char)szUniString[i])<<8+ (szUniString[i+1& 0xff);
        i++;
        int len= UCS2toUTF8Code(iTemp, pTemp);
        if (0 == len)
        {
            return -1;
        }else{
            memcpy((void*)(pUTFString+pos),(const void*)pTemp,len);
            pos+=len;
        }
        memset(pTemp , 0 ,sizeof(pTemp));
    }
    pUTFString[pos]='/0';
    strDest = pUTFString;
    free(pUTFString);
    return 0;
}
// UTF8 to GBK
int API_IC_UTFToGBK(string& str)
{
    const char * rsIn = str.c_str();
    int rsInLength = str.length();
    char buff[str.length() + 128];
    memset(buff, 0sizeof(buff));
    char *rsOut = buff;
    int rsOutLength = str.length() + 128;
    int iLeftRoomLen,iLeftInLen,iOutLen;
    iconv_t stCvt;
    stCvt = iconv_open("GBK""UTF-8");
    if (stCvt == 0)   return -1;
    iLeftInLen = rsInLength;
    iLeftRoomLen = iLeftInLen * 4 + 1;
    iOutLen = iLeftRoomLen;
    char * pszWorkingBuffer = new char[iLeftRoomLen];
    if (pszWorkingBuffer == NULL)
        return -1;
    char * pszOutBuf = pszWorkingBuffer;
    memset(pszWorkingBuffer,0,iLeftRoomLen);
    int iRet;
    char *pInBuf = (char *)rsIn;
    while (iLeftInLen > 0)
    {
        iRet = iconv(stCvt, &pInBuf, (size_t*)&iLeftInLen, &pszWorkingBuffer, (size_t *)&iLeftRoomLen);
        if (iRet == (int)((size_t)-1))
        {
            if (errno == EILSEQ)
            {
                iLeftInLen -= 2;
                pInBuf += 2;
            }
            else
            {
                iconv_close(stCvt);
                delete[] pszOutBuf;
                return -2;
            }
        }
    }
    iconv_close(stCvt);
    pszOutBuf[iOutLen - iLeftRoomLen] = 0;
    rsOutLength = iOutLen - iLeftRoomLen;
    memcpy(rsOut, pszOutBuf, rsOutLength);
    delete[] pszOutBuf;
    str = rsOut;
    return 0;
}

你可能感兴趣的:(C++,c,String,null,delete)