简体   繁体   中英

URL Encoding an Extended ASCII std::string in C++

I have an std::string filled with extended ASCII values (eg čáě ). I need to URL encode this string for JavaScript to decode with DecodeURIComponent .

I have tried converting it to UTF-16 and then to UTF-8 via the windows-1252 codepoint, but wasn't able to do so as there is not enough examples for the MultiByteToWideChar and WideCharToMultiByte functions.

I am compiling with MSVC-14.0 on Windows 10 64-bit.

How can I at least iterate over the individual bytes of the final UTF-8 string for me to URL encode?

Thanks

You can use MultiByteToWideChar to convert the string to UTF-16 and then encode the chars one by one.

Example code:

std::string readData = "Extended ASCII characters (ěščřžýáíé)";
int size = MultiByteToWideChar(
    1252, //1252 corresponds with windows-1252 codepoint
    0,
    readData.c_str(),
    -1, //the string is null terminated, no need to pass the length
    NULL,
    0
);
wchar_t* wchar_cstr = new wchar_t[size];
MultiByteToWideChar(
    1252,
    0,
    readData.c_str(),
    -1,
    wchar_cstr,
    size
);
std::stringstream encodeStream;
for(uint32_t i = 0; i < size; i++){
    wchar_t wchar = wchar_cstr[i];
    uint16_t val = (uint16_t) wchar;
    encodeStream << "%" << std::setfill('0') << std::setw(2) << std::hex << val;
}
delete[] wchar_cstr;

std::string encodedString = encodeStream.str(); // the URL encoded string

While this does encode the basic ASCII characters ( < 128 ) it is completely decodable by JavaScript, which was the end goal.

I managed to do it with quite simple code. Here is an example of converting JSON read from file into URL and sending to an external web site for showing syntax errors in the JSON (tested on MS/Windows):

void EncodeJsonFileTextAndSendToExternalWebSiteToShowSyntaxErrors (const std::string &jsonTxt)
{
        std::stringstream encodeStream;
        for (char c : jsonTxt)
        {
            if (c>='0' && c<='9' || c>='a' && c<='z' || c>='A' && c<='Z' || strchr("{}();",c))
                encodeStream << c;
            else
                encodeStream << "%" << std::setfill('0') << std::setw(2) << std::hex << (int)c;
        }
        std::string url = "cmd /c start https://jsonlint.com/?json=" + encodeStream.str();
        system(url.c_str());
}

which automatically opens a web browser like that: https://jsonlint.com/?json={%0a%22dataset%20name%22%3a%20%22CIHP%22%0alabel%2017%0a}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM