/* src:urf-8 */
#include <iostream>
#include <sstream>
#include <cctype>
std::string txUrlEncode(std::string &input_text) {
std::stringstream work_ss;
for (auto &ch : input_text)
work_ss << '%' << std::hex << (int)(ch + 256) % 256;
std::string output = work_ss.str();
for (auto &c : output) c = (islower(c)) ? toupper(c) : c;
return output;
}
std::string txUrlDecode(std::string &input_text) {
std::string out_text;
for (auto p = input_text.begin(); p != input_text.end(); p++) {
if (*p != '%') {
out_text += *p;
} else {
p++;
char c1 = (*p >= '0' && *p <= '9') ? *p - '0' : (*p >= 'A' && *p <= 'F') ? *p - 'A' + 10 : *p;
c1 <<= 4;
p++;
char c2 = (*p >= '0' && *p <= '9') ? *p - '0' : (*p >= 'A' && *p <= 'F') ? *p - 'A' + 10 : *p;
char c = c1 + c2;
out_text += c;
}
}
return out_text;
}
int main() {
std::string orgtext = "日 本";
std::string encoded = txUrlEncode(orgtext);
std::cout << "encoded: " << encoded << std::endl;
std::string decoded = txUrlDecode(encoded);
std::cout << "decoded: " << decoded << std::endl;
return 0;
}
/* end */
Lyogc3JjOnVyZi04ICovCiNpbmNsdWRlIDxpb3N0cmVhbT4KI2luY2x1ZGUgPHNzdHJlYW0+CiNpbmNsdWRlIDxjY3R5cGU+CgpzdGQ6OnN0cmluZyB0eFVybEVuY29kZShzdGQ6OnN0cmluZyAmaW5wdXRfdGV4dCkgewogIHN0ZDo6c3RyaW5nc3RyZWFtIHdvcmtfc3M7CiAgZm9yIChhdXRvICZjaCA6IGlucHV0X3RleHQpCiAgICB3b3JrX3NzIDw8ICclJyA8PCBzdGQ6OmhleCA8PCAoaW50KShjaCArIDI1NikgJSAyNTY7CiAgc3RkOjpzdHJpbmcgb3V0cHV0ID0gd29ya19zcy5zdHIoKTsKICBmb3IgKGF1dG8gJmMgOiBvdXRwdXQpIGMgPSAoaXNsb3dlcihjKSkgPyB0b3VwcGVyKGMpIDogYzsKICByZXR1cm4gb3V0cHV0Owp9CgpzdGQ6OnN0cmluZyB0eFVybERlY29kZShzdGQ6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