#include <iostream>
#include <string>
// 尝试对字符串进行URL解码(简单的替换%)
std::string try_url_decode(const std::string& encoded_str) {
std::string decoded_str = encoded_str;
for (size_t pos = 0; pos < decoded_str.length(); ++pos) {
if (decoded_str[pos] == '%' && pos + 2 < decoded_str.length()) {
// 尝试将"%XX"格式的字符解码
char hex1 = decoded_str[pos + 1];
char hex2 = decoded_str[pos + 2];
if ((hex1 >= '0' && hex1 <= '9') || (hex1 >= 'A' && hex1 <= 'F') ||
(hex1 >= 'a' && hex1 <= 'f')) {
if ((hex2 >= '0' && hex2 <= '9') || (hex2 >= 'A' && hex2 <= 'F') ||
(hex2 >= 'a' && hex2 <= 'f')) {
// 将"%XX"转换为对应的字符
int decoded_char = (16 * (hex1 >= 'A' ? (hex1 & 0x0F) + 9 :
hex1 >= 'a' ? (hex1 & 0x0F) + 9 :
hex1 - '0')) +
(hex2 >= 'A' ? (hex2 & 0x0F) + 9 :
hex2 >= 'a' ? (hex2 & 0x0F) + 9 :
hex2 - '0');
decoded_str.replace(pos, 3, 1, static_cast<char>(decoded_char));
pos += 2; // 跳过已替换的两个字符
}
}
}
}
return decoded_str;
}
// 判断字符串是否已URL解码
bool is_url_decoded(const std::string& str) {
std::string decoded_str = try_url_decode(str);
return decoded_str == str;
}
int main() {
std::string encoded_str = "我是嘴巴最甜的瓜瓜";
std::string decoded_str = "%cu12";
std::cout << "Encoded string: " << encoded_str << std::endl;
std::cout << "Decoded string: " << decoded_str << std::endl;
if (is_url_decoded(encoded_str)) {
std::cout << "The encoded string is already URL decoded." << std::endl;
}
else {
std::cout << "The encoded string is not URL decoded." << std::endl;
}
if (is_url_decoded(decoded_str)) {
std::cout << "The decoded string is already URL decoded." << std::endl;
}
else {
std::cout << "The decoded string is not URL decoded." << std::endl;
}
return 0;
}
字符decode处理,特别是%cu,%3等极端情况下的处理
需积分: 1 101 浏览量
2024-03-06
15:09:43
上传
评论
收藏 12.22MB ZIP 举报
翊神总裁--九哥
- 粉丝: 182
- 资源: 3
最新资源
- 基于微信小程序+mysql的答题系统的设计与实现+全部资料+详细文档(毕业设计).zip
- H5U的PLC固件,用于EthernetIP通讯时升级固件使用
- Jumpserver堡垒机快速入门视频.zip
- 基于springboot+mysql+redis实现个性化书籍推荐和相似帖子推荐的社区图书馆+论坛+失物招领平台
- 软考中级软件设计师 法律法规通关笔记
- 基于java+sqlserver实现的员工培训系统
- java毕业设计基于Springboot的贫困生资助系统源码+使用文档+全部资料(优秀项目).zip
- 基于随机森林和袋装法对PM2.5浓度进行回归预测
- 静态路由实验.docx
- Rust语言探索之旅:从入门到实践,构建安全高效的系统级应用-文章博客-markdown材料.rar
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈