0

I have an std::string filled with extended ASCII values (e.g. čáě). 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

1
  • Extended ASCII doesn't mean much. Text is encoded with one specific character encoding, either the same one all the time (almost always preferred) or the OS/user/process/thread's current one. So which is it in your program? (If your program uses string literals, then the answer is the one you are telling your compiler to use as the destination charset.) Commented Mar 20, 2018 at 21:33

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

This is not standard C++, but Windows API.
0

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}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.