16進制數據與字符串互轉

近期項目中用到16進制數據與字符串互轉算法,但網絡上搜索到的算法都有些問題,無奈自已造輪子

【注:代碼在VS2015環境下編譯通過】

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);
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章