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

📄 convertasciichrhex.cpp

📁 QQ自动登陆器
💻 CPP
字号:
/********************************************************************
	created:	2007/08/29
	created:	29:8:2007   8:43
	filename: 	ConvertAsciiChrHex.cpp
	file base:	ConvertAsciiChrHex
	file ext:	cpp
	author:		zhupf
	
	purpose:	Hex2AsciiChr & AsciiChr2Hex functions
*********************************************************************/
#include "ConvertAsciiChrHex.h"

// Hex2AsciiChr & AsciiChr2Hex functions
// receive '0'-'F', return 0-15
BYTE AsciiChrToHex(BYTE AsciiChr)
{
	BYTE Hex;
	AsciiChr|=0xFF20;
	if (AsciiChr>='0'&&AsciiChr<='9')
	{
		Hex = (BYTE)(AsciiChr-'0');
	}
	/*
	if (AsciiChr>='A'&&AsciiChr<='F')
	{
	Hex = (BYTE)(AsciiChr-'A')+10;
	}
	*/
	if (AsciiChr>='a'&&AsciiChr<='f')
	{
		Hex = (BYTE)(AsciiChr-'a')+10;
	}
	
	return Hex;
}

// receive 0-15, return '0'-'F'
BYTE HexToAsciiChr(BYTE Hex)
{
	BYTE AsciiChr;
	if (Hex>=0&&Hex<=9)
	{
		AsciiChr = (BYTE)(Hex+'0');
	}
	if (Hex>=10&&Hex<=15)
	{
		AsciiChr = (BYTE)(Hex-10+'A');
	}

	return AsciiChr;
}

// receive "00"-"FF", return 0-255, -1 for error
BYTE AsciiStrToHex(CString AsciiStr, int Start)
{
	if (AsciiStr.GetLength()<Start+2)
	{
		return -1;
	}
	BYTE Hex = BYTE((AsciiChrToHex(AsciiStr.GetAt(Start+0))<<4)+AsciiChrToHex(AsciiStr.GetAt(Start+1)));
	
	return Hex;
}

// receive 0-255, return "00"-"FF";
CString HexToAsciiStr(BYTE Hex)
{
	CString AsciiStr="01";
	AsciiStr.SetAt(1,HexToAsciiChr(Hex&0x0F));
	AsciiStr.SetAt(0,HexToAsciiChr((Hex>>4)&0x0F));

	return AsciiStr;
}

// 2进制内存块转换成16进制文本
void MemToHexStr(const BYTE* mem, DWORD memlen, CString& HexStr)
{
	for (DWORD i = 0; i < memlen; i++)
	{
		HexStr+=HexToAsciiStr(mem[i]);
	}
}

// 16进制文本转换成2进制内存块
bool HexStrToMem(const CString& HexStr, BYTE* mem)
{
	if (HexStr.GetLength()%2 || HexStr!=HexStr.SpanIncluding("0123456789ABCDEFabcdef"))
	{
		return false;
	}
	for (int i = 0; i < HexStr.GetLength(); i+=2)
	{
		mem[i/2] = AsciiStrToHex(HexStr.Mid(i,2));
	}
	return true;
}

// 普通ASCII字符串转换成16进制文本格式字符串
CString AsciiStrToHexStr(CString AsciiStr)
{
	CString HexStr;
	for (int i = 0; i < AsciiStr.GetLength(); i++)
	{
		HexStr+=HexToAsciiStr(AsciiStr[i]);
	}
	return HexStr;
}

// 16进制文本格式字符串转换成普通ASCII字符串
CString HexStrToAsciiStr(CString HexStr)
{
	if (HexStr.GetLength()%2 || HexStr!=HexStr.SpanIncluding("0123456789ABCDEFabcdef"))
	{
		return "";
	}
	CString AsciiStr;
	for (int i = 0; i < HexStr.GetLength(); i+=2)
	{
		AsciiStr+=" ";
		AsciiStr.SetAt(i/2, AsciiStrToHex(HexStr.Mid(i,2)));
	}
	return AsciiStr;
}

// 0x0000 0000 格式地址字符串转换成双字地址
DWORD AddrStrToHex(CString RawAddr)
{
	// 先保证得到不多于8个的字符(不包含'x'和'X')
	if (RawAddr.GetLength()>10)
	{
		return -1;
	}
	
	// 过滤非法字符
	CString ParseAddr = RawAddr.SpanIncluding( "xX0123456789ABCDEFabcdef" );
	if (ParseAddr.Compare(RawAddr))
	{
		return -1;
	}
	
	int HexMark=ParseAddr.FindOneOf("xX");
	if (-1!=HexMark)// 找到
	{
		if (1!=HexMark)
		{
			return -1;
		}
		ParseAddr=ParseAddr.Mid(HexMark+1);
	}
	else
	{
		if (ParseAddr.GetLength()>8)
		{
			return -1;
		}
	}
	int i = 0;
	for (i = 0; i < ParseAddr.GetLength(); i++)
	{
		if (ParseAddr[i]!='0')
		{
			ParseAddr=ParseAddr.Mid(i);
			break;
		}
	}

	DWORD HexAddr = 0;
	for (i = 0; i < ParseAddr.GetLength(); i++)
	{
		HexAddr <<= 4;
		HexAddr |= AsciiChrToHex(ParseAddr[i]);
	}

	return HexAddr;
}

// 双字地址转换成0x0000 0000 格式地址字符串
CString HexAddrToStr(DWORD HexAddr, bool WithPrefix)
{
	CString FixedBaseAdress = _T("00000000");
	CString RawAddr;
	RawAddr.Format("%X", HexAddr);
	RawAddr = FixedBaseAdress.Left(FixedBaseAdress.GetLength()
				-RawAddr.GetLength())+RawAddr;
	if (WithPrefix)
	{
		return "0x"+RawAddr;
	}
	return RawAddr;
}

⌨️ 快捷键说明

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