近期项目中用到16进制数据与字符串互转算法,但网络上搜索到的算法都有些问题,无奈自已造轮子
【注:代码在VS2015环境下编译通过】
【工程下载路径:https://download.youkuaiyun.com/download/cabinriver/10956952】
1、头文件
/**************************************************************************
* @Copyright (c) 2019, ChenMH, All rights reserved.
* @file : HexStrToHex.cpp
* @version : ver 1.0
* @author : ChenMH
* @date : 2019/02/14 09:54
* @brief : 16进制数据与字符串互转算法
**************************************************************************/
#ifndef _ALG_H_
#define _ALG_H_
class Alg
{
public:
/*-----------------------------------------------------*/
/* *
* @brief : 16进制数据转字符
* @input : 16进制数据
* @return : 返回对应的字符
* @author : ChenMH 2019/02/14 11:36
* */
/*-----------------------------------------------------*/
static unsigned char Hex2Char(unsigned char nNum);
/*-----------------------------------------------------*/
/* *
* @brief : 16进制数据转16进制字符串
* @input : pHex 16进制数据数组
nLen 16进制数据长度
* @output : 转换后的16进制字符串
* @return : 返回转换后的字符串长度
* @author : ChenMH 2019/02/14 11:33
* */
/*-----------------------------------------------------*/
static int Hex2HexStr(unsigned char *pHex, int nLen, unsigned char *pHexStr);
/*-----------------------------------------------------*/
/* *
* @brief : 字符转16进制数据
* @input : 字符
* @return : 返回对应的16进制数据,非16进制字符
则返回0x00
* @author : ChenMH 2019/02/14 10:36
* */
/*-----------------------------------------------------*/
static unsigned char Char2Hex(unsigned char c);
/*-----------------------------------------------------*/
/* *
* @brief : 16进制字符串转16进制数据
* @input : strHex 需要转换的16进制字符串
length 需要转换的16进制字符串长度
* @output : out 输出转换后的16进制数据
* @return : 返回转换后的数据长度
* @author : ChenMH 2019/02/14 10:41
* */
/*-----------------------------------------------------*/
static int HexStrToHex(unsigned char *strHex, int length, unsigned char *out);
private:
Alg() {};
~Alg() {};
};
#endif //_ALG_H_
2、源文件
#include "stdafx.h"
#include "Alg.h"
#include <string.h>
unsigned char Alg::Hex2Char(unsigned char nNum)
{
unsigned char temp = 0x00;
if (nNum <= 9)
{
temp = nNum + '0';
}
else
{
/*0 - 9 是十个数*/
temp = (nNum - 10) + 'A';
}
return temp;
}
int Alg::Hex2HexStr(unsigned char *pHex, int nLen, unsigned char *pHexStr)
{
if ((NULL == pHex) || (NULL == pHexStr))
return 0;
int i = 0, j = 0;
unsigned char hBit = 0, lBit = 0;
for (i = 0; i < nLen; ++i)
{
//计算高4位数据
hBit = (pHex[i] >> 4) & 0x0F;
pHexStr[j++] = Hex2Char(hBit);
//计算低4位数据
lBit = pHex[i] & 0x0F;
pHexStr[j++] = Hex2Char(lBit);
}
return j;
}
unsigned char Alg::Char2Hex(unsigned char c)
{
unsigned char temp = 0x00;
//只判断0-9、A-F、a-f;其它字符转为0x00。
if (c > '9')
{
if ((c >= 'A') && (c <= 'F'))
{
temp = c - 0x40 + 0x09;
}
if ((c >= 'a') && (c <= 'f'))
{
temp = c - 0x60 + 0x09;
}
}
else if (('0' <= c) && (c <= '9'))
{
temp = c - 0x30;
}
else
{
goto Func_End_Process;
}
Func_End_Process:
return temp;
}
int Alg::HexStrToHex(unsigned char *strHex, int length, unsigned char *out)
{
if ((NULL == strHex) || (NULL == out))
return 0;
unsigned char *p = strHex;
char hBit = 0, lBit = 0;
int i = 0;
for (; i<(length / 2); ++i, ++p)
{
//分别计算高、低位数据
hBit = Char2Hex(*p);
lBit = Char2Hex(*(++p));
//高低位数据组合
out[i] = ((hBit & 0x0f) << 4 | (lBit & 0x0f));
}
if (0 != (length % 2))
out[i] = Char2Hex(*p);
return (length / 2 + length % 2);
}