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

📄 bin2hexdlg.cpp

📁 将bin文件转换成hex文件
💻 CPP
字号:
// Bin2HexDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Bin2Hex.h"
#include "Bin2HexDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

const char* InitMsg[] =
{
	";aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
	";aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
	";  init Flash F1",
	"00220000C400000A10",
	"00220004C400001501",
	";  init Flash F2",
	"00220008C400000A10",
	"0022000CC400001501",
	";  init combo SRAM",
	"0021B808C400000001",
	"00220018C400000A10",
	"0022001CC400001501",
	";  init SDRAM",
	"00221000C491028200        ; pre charge command",
	"08200000E4                ; special read for pre charge all command",
	"00221000C4A1028200        ; auto refresh",
	"08000000E4                ;  special read",
	"08000000E4                ;  special read",
	"08000000E4                ;  special read",
	"08000000E4                ;  special read",
	"08000000E4                ;  special read",
	"08000000E4                ;  special read",
	"08000000E4                ;  special read",
	"08000000E4                ;  special read",
	"00221000C4B1028200        ; set mode register",
	"08088C00E4                ;  special read",
	"00221000C481028200        ; set to normal mode",
	0,
};

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CBin2HexDlg dialog

CBin2HexDlg::CBin2HexDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CBin2HexDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CBin2HexDlg)
	m_strPath = _T("D:/EXE&BIN/PRJ_UDISK_S.bin");
	//m_Addr = _T("");
	m_Addr = _T("8010000");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CBin2HexDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CBin2HexDlg)
	DDX_Text(pDX, IDC_EDIT2, m_strPath);
	DDX_Text(pDX, IDC_EDIT1, m_Addr);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CBin2HexDlg, CDialog)
	//{{AFX_MSG_MAP(CBin2HexDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_SEL, OnSel)
	ON_BN_CLICKED(IDOK, OnOk)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CBin2HexDlg message handlers

BOOL CBin2HexDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CBin2HexDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CBin2HexDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CBin2HexDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CBin2HexDlg::OnSel() 
{
	// TODO: Add your control notification handler code here
	CFileDialog fd(TRUE, "bin", NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, "Binary Files (*.bin)|*.bin|All Files (*.*)|*.*||", NULL );
	if(IDOK == fd.DoModal())
	{
		m_strPath = fd.GetPathName();
	}

	UpdateData(FALSE);
}

void CBin2HexDlg::OnOk() 
{
	// TODO: Add your control notification handler code here
	CString strSFile,strDFile,strArry;
	UINT addr,saddr;
	unsigned char data[16];
	char tmp[100];
	CString strTmp;
	int len,i;
	const char* p;

	UpdateData();

	sscanf((LPCTSTR)m_Addr,"%x",&addr);
	saddr = addr;

	strSFile = m_strPath;
	strSFile.Replace("\\","\\\\");

	strDFile = m_strPath;
	int k = strDFile.ReverseFind('.');
	strDFile = strDFile.Left(k+1);
	strDFile += "txt";
	strDFile.Replace("\\","\\\\");

	strArry = m_strPath.Left(k);
	k = strArry.ReverseFind('\\');
	k = strArry.GetLength()-k-1;
	strArry = strArry.Right(k);

	CFile SFile,DFile;
	if(!SFile.Open(strSFile,CFile::modeRead))
	{
		MessageBox("Open source file failed");
		return;
	}

	if(!DFile.Open(strDFile,CFile::modeCreate|CFile::modeWrite))
	{
		MessageBox("Create destination file failed");
		return;
	}
/*
	i=0;
	while(InitMsg[i])
	{
		p=InitMsg[i];
		DFile.Write(p,strlen(p));
		DFile.Write("\r\n",2);
		i++;
	}
	DFile.Write("\r\n",2);

	p=";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;\r\n\r\n";
	DFile.Write(p,strlen(p));
*/
	while(1)
	{
		len = SFile.Read(data,16);

		if(len)
		{	
			strTmp = "";
			//strTmp.Format("0%X",addr);
			sprintf(tmp,"%08X",addr);
			strTmp += tmp;
			sprintf(tmp,"%02X",len);
			strTmp += tmp;
			for(i=0;i<len;i++)
			{
				sprintf(tmp,"%02X",data[i]);
				strTmp += tmp;
			}
			strTmp += '\n';

			DFile.Write((LPCTSTR)strTmp,strTmp.GetLength());

			addr+=len;
		}
		else break;
	}

	sprintf(tmp,"%08X00\n",saddr);
	DFile.Write(tmp,strlen(tmp));
	
	SFile.Close();
	DFile.Close();

	MessageBox("转换成功");
}

⌨️ 快捷键说明

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