1

I am trying to achieve URL encoding for some of my strings via c++. Strings can contaim multibyte characters like ™, ®, ©, etc.

Input text: Something ™
Output should be: Something%20%E2%84%A2

I can achieve URL encode or decode in JS with encodeURIComponent and decodeURIComponent, but I have some native code in c++ and hence need to encode some text via c++.

Any help here would be great relief for me.

1 Answer 1

1

It's not to hard to do manually, if you can't find a library. First encode the string as UTF-8 (there are other posts on SO about using the standard library to do that if the string is in another encoding) and then replace every character with a value above 127, and every one that's restricted in URLs, with the percent encoding of that character (A percent sign followed by the two hexadecimal digits representing the character's value).

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

2 Comments

According to a random google search, the restricted characters are ":/?#[]@!$&'()*+,;="
The easiest solution may be to create a std::string replacement[256], and just set replacement['a']="a". @MooingDuck: ISTR non-printable characters under %20 are also restricted, in particular CR, LF and TAB.

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.