C++版本的UnEscape 解析\uxxxx\uxxxx编码字符

解析类似于这种Unicode编码格式的字符串

\u5b55\u5987\u88c5\u590f\u88c52018\u65b0\u6b3e\u5bbd\u677e\u77ed\u8896\u4e2d\u957f\u6b3e\u4e0a\u8863\u96ea\u7ebaV\u9886\u8774\u8776\u7ed3\u8fde\u8863\u88d9\u590f\u5b63

当然JavaScript 直接调用unescape就可以解析 自己动手写了一个c++版本的

代码如下:

#define GB2312_ACP 936
std::string UnEscape(const char* strSource)
{
    std::string strResult;
    int nDestStep = 0;
    int nLength = strlen(strSource);
    if (!nLength || nLength < 6) return strResult;
    char* pResult = new char[nLength + 1];
    wchar_t* pWbuufer = nullptr;
    if (!pResult)
    {
        pResult = NULL;
        return strResult;
    }
    ZeroMemory(pResult, nLength + 1);
    for (int nPos = 0; nPos < nLength;nPos++)
    {
        if (strSource[nPos] == '\\' && strSource[nPos+1]=='u')
        {
            char szTemp[5];
            char szSource[5];
            ZeroMemory(szTemp, 5);
            ZeroMemory(szSource, 5);
            CopyMemory(szSource, (char*)strSource + nPos +2, 4);
            sscanf_s(szSource, "%04X",szTemp);
            CopyMemory(pResult + nDestStep, szTemp, 4);
            nDestStep += 2;
        }
    }
    nDestStep += 2;
    pWbuufer = new wchar_t[nDestStep];
    if (!pWbuufer)
    {
        delete[] pWbuufer;
        pWbuufer = nullptr;
        return strResult;
    }
    ZeroMemory(pWbuufer, nDestStep);
    CopyMemory(pWbuufer, pResult, nDestStep);
    delete [] pResult;
    pResult = nullptr;
    CHAR* MultPtr = nullptr;
    int MultLen = -1;
    //GB2312_ACP = 936
    MultLen = ::WideCharToMultiByte(GB2312_ACP, WC_COMPOSITECHECK, pWbuufer, -1, NULL, NULL, NULL, NULL);
    MultPtr = new CHAR[MultLen + 1];
    if (MultPtr)
    {
        ZeroMemory(MultPtr, MultLen + 1);
        ::WideCharToMultiByte(GB2312_ACP, WC_COMPOSITECHECK, pWbuufer, -1, MultPtr, MultLen, NULL, NULL);
        strResult = MultPtr;
        delete[] MultPtr;
        MultPtr = nullptr;
    }
    delete [] pWbuufer;
    pWbuufer = nullptr;
    return strResult;
}

调用方式:

    char buf[] = "\\u5b55\\u5987\\u88c5\\u590f\\u88c52018\\u65b0\\u6b3e\\u5bbd\\u677e\\u77ed\\u8896\\u4e2d\\u957f\\u6b3e\\u4e0a\\u8863\\u96ea\\u7ebaV\\u9886\\u8774\\u8776\\u7ed3\\u8fde\\u8863\\u88d9\\u590f\\u5b63";

    std::string  nLength = UnEscape(buf);

    printf("%s\r\n", nLength.c_str());

 

posted @ 2018-07-27 10:09  轻风々  阅读(1442)  评论(0编辑  收藏  举报