webCode/Cpp/testHTTP/MyHttp/ztool.hpp
2023-10-14 15:48:44 +08:00

31 lines
867 B
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#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