转载地址:https://www.cnblogs.com/aricgreen/articles/2170238.html
对http的url数据进行编码解码接口:
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#define NON_NUM '0'
char Char2Num(char ch){
if(ch>='0' && ch<='9')return (char)(ch-'0');
if(ch>='a' && ch<='f')return (char)(ch-'a'+10);
if(ch>='A' && ch<='F')return (char)(ch-'A'+10);
return NON_NUM;
}
/************************************************
* 把字符串进行URL编码。
* 输入:
* str: 要编码的字符串
* strSize: 字符串的长度。这样str中可以是二进制数据
* result: 结果缓冲区的地址
* resultSize:结果地址的缓冲区大小(如果str所有字符都编码,该值为strSize*3)
* 返回值:
* >0: result中实际有效的字符长度,
* 0: 编码失败,原因是结果缓冲区result的长度太小
************************************************/
int URLEncode(const char* str, const int strSize, char* result, const int resultSize)
{
int i;
int j = 0; /* for result index */
char ch;
if ((str == NULL) || (result == NULL) || (strSize <= 0) || (resultSize <= 0))
{
return 0;
}
for