查看: 13|回复: 0

    C语言中实现URLEncode和URLDecode

    [复制链接]

    335

    主题

    335

    帖子

    791

    积分

    管理员

    Rank: 9Rank: 9Rank: 9

    积分
    791
    发表于 2023-9-19 16:21:47 | 显示全部楼层 |阅读模式
    C语言中实现URLEncode和URLDecode

    在C语言中,你可以使用各种字符串操作函数来实现URL编码和解码。

    以下是在C中实现URL编码和解码的示例:

    URL编码(URLEncode):

    1. #include <stdio.h>
    2. #include <stdlib.h>
    3. #include <string.h>
    4. #include <ctype.h>
    5. char* urlEncode(const char* str) {
    6.     const char* hex = "0123456789ABCDEF";
    7.     size_t len = strlen(str);
    8.     char* encoded = malloc(3 * len + 1);  // 为编码后的字符串分配内存空间
    9.      size_t j = 0;
    10.     for (size_t i = 0; i < len; i++) {
    11.         if (isalnum((unsigned char)str[i]) || str[i] == '-' || str[i] == '_' || str[i] == '.' || str[i] == '~') {
    12.             encoded[j++] = str[i];
    13.         } else if (str[i] == ' ') {
    14.             encoded[j++] = '+';
    15.         } else {
    16.             encoded[j++] = '%';
    17.             encoded[j++] = hex[(str[i] >> 4) & 0xF];
    18.             encoded[j++] = hex[str[i] & 0xF];
    19.         }
    20.     }
    21.     encoded[j] = '\0';  // 编码后的字符串以空字符结尾
    22.     return encoded;
    23. }
    24. int main() {
    25.     const char* url = "Hello World!";
    26.     char* encodedUrl = urlEncode(url);
    27.     printf("编码后的URL:%s\n", encodedUrl);
    28.     free(encodedUrl);  // 记得释放分配的内存空间
    29.     return 0;
    30. }
    复制代码

    URL解码(URLDecode):

    1. #include <stdio.h>
    2. #include <stdlib.h>
    3. #include <string.h>
    4. #include <ctype.h>
    5. char* urlDecode(const char* str) {
    6.     size_t len = strlen(str);
    7.     char* decoded = malloc(len + 1);  // 为解码后的字符串分配内存空间
    8.      size_t j = 0;
    9.     for (size_t i = 0; i < len; i++) {
    10.         if (str[i] == '+') {
    11.             decoded[j++] = ' ';
    12.         } else if (str[i] == '%') {
    13.             if (isxdigit((unsigned char)str[i + 1]) && isxdigit((unsigned char)str[i + 2])) {
    14.                 char hex[3] = {str[i + 1], str[i + 2], '\0'};
    15.                 decoded[j++] = strtol(hex, NULL, 16);
    16.                 i += 2;
    17.             } else {
    18.                 decoded[j++] = str[i];
    19.             }
    20.         } else {
    21.             decoded[j++] = str[i];
    22.         }
    23.     }
    24.     decoded[j] = '\0';  // 解码后的字符串以空字符结尾
    25.     return decoded;
    26. }
    27. int main() {
    28.     const char* encodedUrl = "Hello%20World%21";
    29.     char* decodedUrl = urlDecode(encodedUrl);
    30.     printf("解码后的URL:%s\n", decodedUrl);
    31.     free(decodedUrl);  // 记得释放分配的内存空间
    32.     return 0;
    33. }
    复制代码

    这些示例演示了如何在C语言中执行URL编码和解码。 urlEncode 函数用于对给定的字符串进行编码,而 urlDecode 函数用于解码给定的URL编码字符串。记得在使用后释放分配的内存空间,以避免内存泄漏。

    另外在VC++中实现上面的功能,可以看一下另一篇文章:

    VC++中实现URLEncode和URLDecode



    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    免责声明:
    1、转载或引用本网站内容须注明原网址,并标明本网站网址“源码资源网”
    2、转载或引用本网站中的署名文章,请按规定向原作者支付稿酬
    3、对于不当转载或引用本网站内容而引起的民事纷争、行政处理或其他损失,本网站不承担责任
    4、对不遵守本声明或其他违法、恶意使用本网站内容者,本网站保留追究其法律责任的权利
    5、资源部分来自网络,不保证资源的完整性,仅供学习研究,如需运营请购买正版,如有侵权请联系客服删除
    6、本站所有资源不带技术支持,下载资源请24小时内删除,如用于违法用途,或者商业用途,一律用于者承担

    QQ|手机版|小黑屋|依星源码资源网-分享编程干货的网站 ( 鲁ICP备2021043233号-3 )

    GMT+8, 2023-10-2 00:40

    邮箱:312337667@qq.com 客服QQ:312337667(工作时间:7:00~23:00)

    © Powered by Net188.com X3.4

    快速回复 返回顶部 返回列表