webCode/Cpp/testHTTP/MyHttp/ztool.hpp

31 lines
867 B
C++
Raw Normal View History

2023-10-14 07:48:44 +00:00
#include <iostream>
#include <sstream>
#include <string>
namespace ztool {
// 用于将URL编码的特殊字符转换为对应的ASCII字符
std::string URLDecode(const std::string &encoded) {
std::ostringstream decoded;
std::string::const_iterator it = encoded.begin();
while (it != encoded.end()) {
char c = *it++;
if (c == '%' && it != encoded.end()) {
// 将%后面的两个字符解析为16进制数并将其转换为字符
int hexValue;
std::istringstream hexStream(std::string(it, it + 2));
if (hexStream >> std::hex >> hexValue) {
decoded << static_cast<char>(hexValue);
it += 2;
} else {
// 解码失败,保留原始字符
decoded << c;
}
} else {
// 非%字符,保留原始字符
decoded << c;
}
}
return decoded.str();
}
} // namespace ztool