1.1、Base16编/解码接口定义
#ifndef BASE_16_H
#define BASE_16_H
#include <stdlib.h>
#include <stdbool.h>
#ifdef __cplusplus
extern "C" {
#endif
/*base16编码
*bytes 要编码的字节的起始指针
*bytesLength 要编码的字节的长度
*isToUpper 是否编码为大写字母
*/
char* base16_encode (unsigned char *bytes, size_t bytesLength, bool isToUpper);
char* base16_encode_to_upper(unsigned char *bytes, size_t bytesLength);
char* base16_encode_to_lower(unsigned char *bytes, size_t bytesLength);
/*base16解码
*hex base16编码的字符串
*/
unsigned char* base16_decode(char *hex);
#ifdef __cplusplus
}
#endif
#endif
1.2、Base16编码实现
#include"base16.h"
char* base16_encode_to_upper(unsigned char* bytes, size_t bytesLength) {
return base16_encode(bytes, bytesLength, true);
}
char* base16_encode_to_lower(unsigned char* bytes, size_t bytesLength) {
return base16_encode(bytes, bytesLength, false);
}
char* base16_encode(unsigned char* bytes, size_t bytesLength, bool isToUpper) {
const char *table = isToUpper ? "0123456789ABCDEF" : "0123456789abcdef";
unsigned char highByte, lowByte;
char *output = (char *)calloc(bytesLength << 1, sizeof(char));
for (size_t i = 0; i < bytesLength; i++) {
//向右移动4bit,获得高4bit
highByte = bytes[i] >> 4;
//与0x0f做位与运算,获得低4bit
lowByte = bytes[i] & 0x0F;
//由于高4bit和低4bit都只有4个bit,他们转换成10进制的数字,范围都在0 ~ 15闭区间内
//大端模式
size_t j = i << 1;
output[j] = table[highByte];
output[j + 1] = table[lowByte];
}
return output;
}
1.3、Base16解码实现
#include <string.h>
#include "base16.h"
//把16进制字符转换成10进制表示的数字
//通过man ascii命令查看ASCII编码表即可得到如下转换逻辑
short hex2dec(char c) {
if ('0' <= c && c <= '9') {
return c - '0';
} else if ('a' <= c && c <= 'f') {
return c - 'a' + 10;
} else if ('A' <= c && c <= 'F') {
return c - 'A' + 10;
} else {
return 0;
}
}
//input指向的字符串长度必须是2的整数倍
unsigned char* base16_decode(char* hex) {
size_t inputLength = strlen(hex);
size_t halfInputLength = inputLength >> 1;
unsigned char *output = (unsigned char *)calloc(halfInputLength, sizeof(unsigned char));
for (size_t i = 0; i < halfInputLength; i++) {
//16进制数字转换为10进制数字的过程
size_t j = i << 1;
output[i] = (hex2dec(hex[j]) << 4) + hex2dec(hex[j + 1]);
}
return output;
}