源码
//
// Created by oceanstar on 2021/8/13.
//
#ifndef OCEANSTAR_HTTP_ACL_HEX_CODE_H
#define OCEANSTAR_HTTP_ACL_HEX_CODE_H
namespace oceanstar{
/**
* 将二进制数据进行编码,一个字节转换成两个字节后,从而转为文本字符串
* @param ptr {const char*} 二进制数据
* @param len {int} ptr 数据的长度
* @return {ACL_VSTRING*} 转换结果,必须调用free释放
*/
unsigned char *acl_hex_encode(const char *in, int len);
/**
* 将编码后的数据进行解码
* @param ptr {const char*} 编码数据
* @param len {int} ptr 数据长度
* @return {ACL_VSTRING*} 解码结果:
* 非NULL表示成功,这时必须调用free释放
* 为NULL表示解码失败,原因是参数错误
*/
unsigned char * acl_hex_decode(const char *in, int len);
}
#endif //OCEANSTAR_HTTP_ACL_HEX_CODE_H
//
// Created by oceanstar on 2021/8/13.
//
#include <cstdlib>
#include "acl_hex_code.h"
namespace oceanstar{
#define UCHAR_PTR(x) ((const unsigned char *)(x))
static const unsigned char acl_hex_chars[] = "0123456789ABCDEF";
/**
* 将二进制数据进行编码,一个字节转换成两个字节后,从而转为文本字符串
* @param ptr {const char*} 二进制数据
* @param len {int} ptr 数据的长度
* @return {ACL_VSTRING*} 转换结果,必须调用free释放
*/
unsigned char *acl_hex_encode(const char *in, int len)
{
unsigned char * out = (unsigned char *)malloc(len * 2 + 1);
int out_index = 0;
const unsigned char *cp;
int ch;
int count;
for (cp = UCHAR_PTR(in), count = len; count > 0; count--, cp++) {
ch = *cp;
out[out_index++] = acl_hex_chars[(ch >> 4) & 0xf];
out[out_index++] = acl_hex_chars[ch & 0xf];
}
out[out_index] = 0;
return (out);
}
/**
* 将编码后的数据进行解码
* @param ptr {const char*} 编码数据
* @param len {int} ptr 数据长度
* @return {ACL_VSTRING*} 解码结果:
* 非NULL表示成功,这时必须调用free释放
* 为NULL表示解码失败,原因是参数错误
*/
unsigned char * acl_hex_decode(const char *in, int len){
unsigned char * out = (unsigned char *)malloc(len / 2 + 1);
int out_index = 0;
const unsigned char *cp;
int count;
unsigned int hex;
unsigned int bin;
for (cp = UCHAR_PTR(in), count = len; count > 0; cp += 2, count -= 2) {
if (count < 2)
return (0);
hex = cp[0];
if (hex >= '0' && hex <= '9')
bin = (hex - '0') << 4;
else if (hex >= 'A' && hex <= 'F')
bin = (hex - 'A' + 10) << 4;
else if (hex >= 'a' && hex <= 'f')
bin = (hex - 'a' + 10) << 4;
else
return (0);
hex = cp[1];
if (hex >= '0' && hex <= '9')
bin |= (hex - '0') ;
else if (hex >= 'A' && hex <= 'F')
bin |= (hex - 'A' + 10) ;
else if (hex >= 'a' && hex <= 'f')
bin |= (hex - 'a' + 10) ;
else
return (0);
out[out_index++] = bin;
}
out[out_index] = 0;
return out;
}
}
测试
int main(void){
const char * key = "aaaaa";
char *enc = (char *)acl_hex_encode(key, strlen(key));
char *dec = (char *)acl_hex_decode(enc, strlen(enc));
printf("enc: [%s], dec: [%s]\n", enc, dec);
free(dec);
free(enc);
return 0;
}