16进制数据与字符串互转

近期项目中用到16进制数据与字符串互转算法,但网络上搜索到的算法都有些问题,无奈自已造轮子

【注:代码在VS2015环境下编译通过】

【工程下载路径:https://download.csdn.net/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);
}

 

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

cabinriver

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值