Skip to content

Instantly share code, notes, and snippets.

@halloweeks
Last active January 16, 2023 12:49
Show Gist options
  • Save halloweeks/d21ee0d2e55347d6a45091795cd3b8d9 to your computer and use it in GitHub Desktop.
Save halloweeks/d21ee0d2e55347d6a45091795cd3b8d9 to your computer and use it in GitHub Desktop.
c++ url decoder
#include <iostream>
#include <stdlib.h>
#include <string>
std::string url_decode(char *url) {
std::string temp, output;
char ch;
for (unsigned int index = 0; index < strlen(url)+1; index++) {
if (url[index] == '%') {
temp += url[index+1];
temp += url[index+2];
ch = std::stoul(temp, nullptr, 16);
output += ch;
temp = "";
index+=2;
} else if (url[index] == '+') {
output += " ";
} else {
output += url[index];
}
}
return output;
}
int main() {
char url[] = "hello%40%23%24_%26-%2B()%2F*%22%27%3A%3B!%3F%2C.~%60%7C%E2%80%A2%E2%88%9A%CF%80%C3%B7%C3%97%C2%B6%E2%88%86%C2%A3%C2%A2%E2%82%AC%C2%A5%5E%C2%B0%3D%7B%7D%5C%25%C2%A9%C2%AE%E2%84%A2%E2%9C%93%5B%5D%3C%3E%20++%5Bweeks";
std::cout << url_decode(url) << "\n";
return 0;
}
@halloweeks
Copy link
Author

halloweeks commented Jan 16, 2023

Compile:

g++ main.cpp -o $HOME/main

Output:
./main
hello@#$_&-+()/*"':;!?,.~`|•√π÷׶∆£¢€¥^°={}%©®™✓[]<> [weeks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment