windows平台编码转换

int AsciiToUtf8(char* pSrc, unsigned int nSrcLen, char* pBuffer, unsigned int nBufferLen)
{
    assert(pSrc != NULL);

    int nRet(0);
    int nUtf16Len = MultiByteToWideChar(CP_ACP, 0, pSrc, nSrcLen, NULL, 0);
    if(nUtf16Len > 0)
    {
        wchar_t* pW = new wchar_t[nUtf16Len];
        if(pW != NULL)
        {
            nUtf16Len = MultiByteToWideChar(CP_ACP, 0, pSrc, nSrcLen, pW, nUtf16Len);

            if(pBuffer == NULL || nBufferLen <= 0)
            {
                nRet = WideCharToMultiByte(CP_UTF8, 0, pW, nUtf16Len, NULL, 0, NULL, NULL);
            }
            else
            {
                nRet = WideCharToMultiByte(CP_UTF8, 0, pW, nUtf16Len, pBuffer, nBufferLen, NULL, NULL);
            }

            delete []pW;
            pW = NULL;
        }
    }

    return nRet;
}

 

posted @ 2020-06-22 19:14  jiangcheng_15  阅读(261)  评论(0编辑  收藏  举报