www.pudn.com > C8051JTAG_SRC.rar > hex2bin.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;iulMaxLen) 
			{ 
				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