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

📄 hex2bin.cpp

📁 C8051F020的下载调试软件和下载线制作电路图。有兴趣做020的朋友可以参考参考。
💻 CPP
字号:
// hex2bin.cpp: implementation of the hex2bin class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "C8051JTAG.h"
#include "hex2bin.h"

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

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

hex2bin::hex2bin()
{

}

hex2bin::~hex2bin()
{
	
}

void hex2bin::SetFile(CString *strFilePath,bool bType)
{

	unsigned long i;
	if (bType)
	{
		SaveHex2Bin(strFilePath);
	}
}

int hex2bin::SaveHex2Bin(CString *strFilePath)
{
	unsigned int j,i,k,uiTemp,uiSum;
	unsigned long ulMaxLen,ulFileCnt;
	char *ucp,*Head;
	char *pWbuf,*pWhead;
	char ucTemp,ucSum;
	bool bEnd;


	CFile myFile;
	CFileException fileException;

	if ( !myFile.Open( *strFilePath, CFile::modeRead, &fileException) )
	{
		TRACE( "Can't open file %s, error = %u\n",
		   strFilePath, fileException.m_cause );
		return 9;
	}


	ucp = (char *)malloc(50);
	Head = ucp;
	i = myFile.GetLength();
	ulMaxLen = 0;
	bEnd = false;
	//计算文件的长度
	while (i)
	{
		
		myFile.Read(ucp,1);
		i--;
		if (*ucp == ':')
		{
			myFile.Read(ucp,2);
			i = i-2;
			uiTemp = atih(ucp,2);

			myFile.Read(ucp,4);
			i = i-4;

			uiTemp += atih(ucp,4);
			if (uiTemp>ulMaxLen)
			{
				ulMaxLen = uiTemp;
			}	
		}
//		ucp++;
	}
	TRACE("最大字节数=%X",ulMaxLen);

	//设置数组,读取文件写入字节
	pWhead = (char *)malloc(ulMaxLen);
	pWbuf = pWhead;
	memset(pWbuf,0,ulMaxLen);
	myFile.Seek(0,CFile::begin);
	ulFileCnt = 0;
	while(!bEnd)
	{
		ulFileCnt++;
		pWbuf =  pWhead;
		myFile.Read(ucp,1);
		uiTemp = 0;
		if (*ucp == ':')
		{
			uiSum = 0;
			myFile.Read(ucp,2);						//读取长度
			j = atih(ucp,2);
			uiSum += j;
			myFile.Read(ucp,4);						//读取地址值
			uiTemp= atih(ucp,4);
			uiSum +=uiTemp/256;
			uiSum +=(unsigned char)uiTemp;
			myFile.Read(ucp,2);						//读取类型
			k = atih(ucp,2);						//
			uiSum +=k;
			if (k!=00)								//非数据类型--结束
			{
				bEnd = true;
				continue;
			}
			for(i=0;i<j;i++)
			{
				myFile.Read(ucp,2);
				ucTemp = atih(ucp,2);
				*(pWbuf+uiTemp+i) = ucTemp;
				uiSum +=ucTemp;
			}
			ucSum = (unsigned char)uiSum;
			ucSum = ~ucSum;
			ucSum++;

			myFile.Read(ucp,2);
			ucTemp = atih(ucp,2);
			if (ucSum!=ucTemp)
			{
				TRACE("校验和出错!!!文件中=%d;计算=%d",ucTemp,ucSum);
				return 1;

			}
			if (ulFileCnt>ulMaxLen)
			{
				TRACE("读取长度超出最大长度!!!读取长度=%d",ulFileCnt);
				return 2;
			}

		}
	}

	//写入文件!BIN格式
	CString strTemp;
	strTemp = myFile.GetFileName();
	i = strTemp.GetLength();
	strTemp.Delete(i-3,3);
	strTemp+="bin";
	TCHAR     temppath[MAX_PATH];
	GetTempPath(MAX_PATH,temppath);
	_tcscat(temppath,strTemp);
	myFile.Close();
	if ( !myFile.Open( temppath, CFile::modeCreate|CFile::modeReadWrite, &fileException) )
	{
		TRACE( "Can't open file %s, error = %u\n",
		   strTemp, fileException.m_cause );
	}
	else
	{
		myFile.Write(pWhead,ulMaxLen);
		myFile.Close();
		strFilePath->Format("%s",temppath);
	}
	
	free(Head);
	free(pWhead);
	return 0;
}


inline char  hex2bin::ConverHexChar(char ch)
{
	if((ch>='0')&&(ch<='9'))
		return ch-0x30;
	else
		if ((ch>='A')&&(ch<='F'))
		{
			return ch-'A'+10;
		}
		else
			if ((ch>='a')&&(ch<='f'))
			{
				return ch-'a'+10;
			}
			else
				return -1;

}



inline unsigned long hex2bin::atih(char *p,int iLen)
{
	//将数字转化位HEX
	unsigned long ulTemp;
	int i,j,m;
	char k;
	char chtemp;

	i = iLen;
	m = 0;
	for(ulTemp = 0,j = 0;j<i;j++,p++)
	{
		chtemp = *p;
		if (chtemp==' ')
		{
			p++;
			continue;
		}

		k = ConverHexChar(chtemp);
		if (k==-1)
		{
			return 0;
		}
		ulTemp = ulTemp *16;
		ulTemp += k;
	}
	return ulTemp;
}
unsigned long hex2bin::atih(CString strTemp)
{
	//将数字转化位HEX
	unsigned long ulTemp;
	int i,j,m;
	char k;
	char chtemp;

	i = strTemp.GetLength();
	m = 0;
	for(ulTemp = 0,j = 0;j<i;j++)
	{
		chtemp = strTemp[j];
		if (chtemp==' ')
		{
			j++;
			continue;
		}

		k = ConverHexChar(chtemp);
		if (k==-1)
		{
			return 0;
		}
		ulTemp = ulTemp *16;
		ulTemp += k;
	}
	return ulTemp;
}

⌨️ 快捷键说明

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