%E4%BC%A0%E5%A5%87%E7%A7%81%E6%9C%8D 这种类型的代码 是什么码,怎么能转换成正常的?求大神指点。

如题所述

这个叫做url encoding,即URL编码,在网址上或者POST数据的时候用,目的是防止文字内容里面的=、?等特殊字符干扰query string的结构,避免解析错误


你给出的“%E4%BC%A0%E5%A5%87%E7%A7%81%E6%9C%8D”解码后就是汉字“传奇私服”


在百度上搜索“url encode”能找到这种编码的在线解析应用


Java语言使用apache commons codec库也可以对其进行编码或解码


C语言中可手动实现编码与解码:

#include <stdlib.h>  
#include <string.h>  
#include <ctype.h>  
#include <sys/types.h>  
  
#include "url.h"  
  
static unsigned char hexchars[] = "0123456789ABCDEF";  
  
static int php_htoi(char *s)  
{  
    int value;  
    int c;  
  
    c = ((unsigned char *)s)[0];  
    if (isupper(c))  
        c = tolower(c);  
    value = (c >= '0' && c <= '9' ? c - '0' : c - 'a' + 10) * 16;  
  
    c = ((unsigned char *)s)[1];  
    if (isupper(c))  
        c = tolower(c);  
    value += c >= '0' && c <= '9' ? c - '0' : c - 'a' + 10;  
  
    return (value);  
}  
  
  
char *php_url_encode(char const *s, int len, int *new_length)  
{  
    register unsigned char c;  
    unsigned char *to, *start;  
    unsigned char const *from, *end;  
      
    from = (unsigned char *)s;  
    end  = (unsigned char *)s + len;  
    start = to = (unsigned char *) calloc(1, 3*len+1);  
  
    while (from < end)   
    {  
        c = *from++;  
  
        if (c == ' ')   
        {  
            *to++ = '+';  
        }   
        else if ((c < '0' && c != '-' && c != '.') ||  
                 (c < 'A' && c > '9') ||  
                 (c > 'Z' && c < 'a' && c != '_') ||  
                 (c > 'z'))   
        {  
            to[0] = '%';  
            to[1] = hexchars[c >> 4];  
            to[2] = hexchars[c & 15];  
            to += 3;  
        }  
        else   
        {  
            *to++ = c;  
        }  
    }  
    *to = 0;  
    if (new_length)   
    {  
        *new_length = to - start;  
    }  
    return (char *) start;  
}  
  
  
int php_url_decode(char *str, int len)  
{  
    char *dest = str;  
    char *data = str;  
  
    while (len--)   
    {  
        if (*data == '+')   
        {  
            *dest = ' ';  
        }  
        else if (*data == '%' && len >= 2 && isxdigit((int) *(data + 1)) && isxdigit((int) *(data + 2)))   
        {  
            *dest = (char) php_htoi(data + 1);  
            data += 2;  
            len -= 2;  
        }   
        else   
        {  
            *dest = *data;  
        }  
        data++;  
        dest++;  
    }  
    *dest = '/0';
  // *dest =0; 用这个好些
    return dest - str;  
}

温馨提示:答案为网友推荐,仅供参考
相似回答