⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hexstrtobyte.cpp

📁 实现对字符串和整篇文章的AES加密和解密操作
💻 CPP
字号:
// HexStrToByte.cpp : implementation file
//

#include "stdafx.h"
#include "HexStrToByte.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CHexStrToByte

CHexStrToByte::CHexStrToByte()
{
}

CHexStrToByte::~CHexStrToByte()
{
}


BEGIN_MESSAGE_MAP(CHexStrToByte, CEdit)
//{{AFX_MSG_MAP(CHexStrToByte)
// NOTE - the ClassWizard will add and remove mapping macros here.
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CHexStrToByte message handlers

//把一个任意长度的十六进制字符串(偶数)转换成对应的值存在一个BYTE型数组out中
//返回值为十六进制字符串中错误的位置,无错误返回0
int CHexStrToByte::SetHexStrToByte(CString hex, BYTE *out)
{
	CString str;
	CString strTemp;
    
	strTemp=hex;
	int hexLong=hex.GetLength();
	if (hexLong%2!=0)
	{
		MessageBox("字符串长度不是2的整数倍,请重试!");
		return FALSE;
	}
	for(int i=0;i<hexLong/2;i++)
	{
		str=strTemp.Left(2);
		strTemp=strTemp.Right(strTemp.GetLength()-2);
		int k=0;
		k=HexStrToDeci(str);
		if (k<0)
		{
			str.Format("您输入的第 %d 个十六进制数不是十六进制字符!",i+1);
			MessageBox(str,"出错提示");
			return i+1;
		}
		out[i]=(BYTE)k;	
	}
	
	return 0;
}
///////////////////////////////////////////////////////////////////////
//把一个十六进制字符转换为对应的整数,错误返回-1
int CHexStrToByte::HexStrToDeci(CString hex)
{
	
	int deci=0;
	if (!(IsHex(hex[0])&&IsHex(hex[1])))
	{
	/*	MessageBox("输入的不是十六进制的字符");*/
		return -1;
	}
	if (_istdigit(hex[0]))
	{
		if (_istdigit(hex[1]))
		{
			sscanf(hex,"%x",&deci);
		}else
			deci=(int)(hex[0]-'0')*16+SingleHexStrToInt(hex[1]);
		
	}
	else if (_istdigit(hex[1]))
	{
		deci=SingleHexStrToInt(hex[0])*16+(int)(hex[1]-'0');
	}else
		deci=SingleHexStrToInt(hex[0])*16+SingleHexStrToInt(hex[1]);
	
	return deci;
}
///////////////////////////////////////////////////////////////////
//把十六进制中的字母转换为对应的整数值返回
int CHexStrToByte::SingleHexStrToInt(char hex)
{
	switch(hex)
	{
	case 'a':
		return 10;
	case 'b':
		return 11;
	case 'c':
		return 12;
	case 'd':
		return 13;
	case 'e':
		return 14;
	case 'f':
		return 15;
	case 'A':
		return 10;
	case 'B':
		return 11;
	case 'C':
		return 12;
	case 'D':
		return 13;
	case 'E':
		return 14;
	case 'F':
		return 15;
	default:
		return 0;
	}
}
/////////////////////////////////////////////////////////////////
//判断是否是十六进制字符
BOOL CHexStrToByte::IsHex(char hex)
{
	if (hex>='0'&& hex<='9')
	{
		return TRUE;
	}else if (hex>='a'&& hex<='f')
	{
		return TRUE;
	}else if (hex>='A'&& hex<='F')
	{
		return TRUE;
	}else 
		return FALSE;
	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -