-
Notifications
You must be signed in to change notification settings - Fork 1
/
URLEncoding.cpp
34 lines (32 loc) · 842 Bytes
/
URLEncoding.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include "URLEncoding.h"
String urlEncode(String source) {
String encodedString = "";
for (int i = 0; i < source.length(); i++) {
char c = source.charAt(i);
if (isAlphaNumeric(c)) {
encodedString += c;
} else if (c == ' ') {
encodedString += "+";
} else {
encodedString += String("%") + String(((int)c), HEX);
}
}
return encodedString;
}
String urlDecode(String source) {
String decodedString = "";
for (int i = 0; i < source.length(); i++) {
char c = source.charAt(i);
if (c == '%') {
String codeChars = source.substring(i+1, i+3);
int code = (int) strtol(codeChars.c_str(), 0, 16);
decodedString += ((char) code);
i += 2;
} else if (c == '+') {
decodedString += " ";
} else {
decodedString += c;
}
}
return decodedString;
}