Skip to content

Instantly share code, notes, and snippets.

@bestpika
Last active July 1, 2018 06:37
Show Gist options
  • Save bestpika/bb520b9c4842553fa74ea8bc2c41ed45 to your computer and use it in GitHub Desktop.
Save bestpika/bb520b9c4842553fa74ea8bc2c41ed45 to your computer and use it in GitHub Desktop.
#arduino 網址解碼
// 文字轉換
unsigned char hs2i(char c)
{
if ('0' <= c && c <= '9') {
return ((unsigned char)c - '0');
} else if ('a' <= c && c <= 'f') {
return ((unsigned char)c - 'a' + 10);
} else if ('A' <= c && c <= 'F') {
return ((unsigned char)c - 'A' + 10);
} else {
return 0;
}
}
// 網址解碼
String decodeUri(String s) {
String a = "";
char c;
std::pair<char, char> h;
for (int i = 0; i < s.length(); i++) {
c = s.charAt(i);
if (c == '+') {
a += ' ';
} else if (c == '%') {
h.first = s.charAt(++i);
h.second = s.charAt(++i);
c = (hs2i(h.first) << 4) | hs2i(h.second);
a += c;
} else {
a += c;
}
yield();
}
return a;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment