#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <sstream>
std::string urlEncode(const std::string& strInput)
{
static auto funcCheckAlpha = [=](unsigned char c) {
bool bAlpha = false;
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9'))
bAlpha = true;
return bAlpha;
};
std::ostringstream osOut;
osOut.fill('0');
osOut << std::hex;
for (auto c : strInput) {
if (funcCheckAlpha(c) || c == '-' || c == '_' || c == '.' || c == '~') {
osOut << c;
}
else if (c == ' ') {
osOut << '+';
}
else {
osOut << '%' << std::setw(2) << int((unsigned char)c);
}
}
return osOut.str();
}
std::string urlDecode(const std::string& strInput) {
std::ostringstream osOut;
for (size_t i = 0; i < strInput.length(); ++i) {
if (strInput[i] == '%') {
int hex;
sscanf(strInput.substr(i + 1, 2).c_str(), "%x", &hex);
osOut << static_cast<char>(hex);
i += 2;
}
else if (strInput[i] == '+') {
osOut << ' ';
}
else {
osOut << strInput[i];
}
}
return osOut.str();
}
unsigned char CharToHex(unsigned char x)
{
return (unsigned char)(x > 9 ? x + 55 : x + 48);
}
int main() {
//std::string strInput = "%E4%BD%A0%E5%A5%BD22";
//std::string strInput("WeCom_4.1.16.6007%281%29--.exe");
//std::string strInput = "%E5%91%98%E5%B7%A5++%E5%AF%86%E7%A0%81%E9%87%8D%E7%BD%AE%E8%BD%AF%E4%BB%B6%EF%BC%881%EF%BC%89.zip";
std::string strInput("WeCom_4.1.16.6007%281%29--.exe");
//std::string strInput = "%e4%bd%a0%e5%a5%bd%e5%95%8a22%e5%95%8a";
std::string decoded = urlDecode(strInput);
std::ofstream file("output.txt");
if (file.is_open()) {
file << "Src: \n\t" << strInput << "\n";
file << decoded << "\n";
std::cout << "File written successfully." << std::endl;
}
else {
std::cout << "Failed to open file." << std::endl;
}
std::string encode = urlEncode(decoded);
file << "encode: \n\t" << encode << "\n";
file.close();
return 0;
}
07-16