Click here to Skip to main content
15,033,303 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

I'm getting JSON message with string like this:
"\u96fb\u5b50\u66f8\u7c4d\u300eThis is \u82f1\u6587\u6cd5\u300f\uff5e\u4e2d"

How can I convert it to readable format and assign to CString?

Thank you

1 solution

Just loop through your string getting the hex values, converting them to binary and storing the binary values to a buffer.

When the string contains UTF-16 codes, the parser might look like this (untested, no error checks):

void MyConversion(CStringW str&, const char *sSrc)
    size_t nSize = strlen(s);
    wchar_t lpszBuf = new wchar_t[nSize + 1];
    size_t i = 0;
    while (*s && i < nSize)
        if ('\\' == s[0] && 'u' == s[1])
            // This assumes that all hex codes consist of four characters
            char cSave = s[6];
            s[6] = 0;
            lpszBuf[i++] = strtoul(s + 2, NULL, 16);
            s += 6;
            *s = cSave;
            lpszBuf[i++] = *s++;
    lpszBuf[i] = 0;
    str = lpszBuf;
    delete [] lpszBuf;

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900