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

📄 binfileexdlg.cpp

📁 二进制文件的排列转换,对于ARM开发人员有用
💻 CPP
字号:
// BinFileExDlg.cpp : implementation file
//

#include "stdafx.h"
#include "BinFileEx.h"
#include "BinFileExDlg.h"

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

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

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()

/////////////////////////////////////////////////////////////////////////////
// CBinFileExDlg dialog

CBinFileExDlg::CBinFileExDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CBinFileExDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CBinFileExDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CBinFileExDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CBinFileExDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CBinFileExDlg, CDialog)
	//{{AFX_MSG_MAP(CBinFileExDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_OPEN_FILE, OnOpenFile)
	ON_BN_CLICKED(IDC_CONVERT1, OnConvert1)
	ON_BN_CLICKED(IDC_CONVERT2, OnConvert2)
	ON_BN_CLICKED(IDC_CONVERT3, OnConvert3)
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CBinFileExDlg message handlers

BOOL CBinFileExDlg::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 CBinFileExDlg::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 CBinFileExDlg::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 CBinFileExDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CBinFileExDlg::OnOpenFile() 
{
	CFileDialog dlg(true);
	if(dlg.DoModal()==IDOK)
	{
		s_SourceFile = dlg.GetPathName();
		CEdit* pEdit1 = (CEdit*)GetDlgItem(IDC_SOURCE_FILE);
		pEdit1->SetWindowText(s_SourceFile);
		pEdit1 = (CEdit*)GetDlgItem(IDC_DEST_FILE);
		pEdit1->SetWindowText(s_SourceFile+".new");
	}
}
int hextoint(char *src, int digits)
{
    int number = 0;
    char ch;

    while (digits-- && *src) {
        ch = *src++;
        if (ch >= '0' && ch <= '9')
            ch = ch - '0';
        else if (ch >= 'A' && ch <= 'F')
            ch = ch - 'A' + 10;
        else if (ch >= 'a' && ch <= 'f')
            ch = ch - 'a' + 10;
        else
            break;
        number = (number << 4) + ch;
    }
    return number;
}
void CBinFileExDlg::OnConvert1() 
{
	CFileDialog dlg(TRUE);	
	dlg.m_ofn.lpstrTitle = "打开要整理的文件";
	dlg.m_ofn.lpstrFilter = "BIN (*.bin)\0*.bin\0";    	
	if(dlg.DoModal()!=IDOK)
		return ;
	CString infilename = dlg.GetPathName();
	CString outfilename = infilename.Left(infilename.GetLength()-4)+"out.bin";
	
	CFile infile,outfile;
	infile.Open(infilename,CFile::modeRead| CFile::shareExclusive,NULL);
	outfile.Open(outfilename,CFile::modeWrite| CFile::modeCreate,NULL);
	int file_size = infile.GetLength();
	BYTE* buf;
	buf = (LPBYTE)malloc(sizeof(BYTE)*file_size);
	infile.Read(buf,file_size);
	BYTE a;
	for(int i=0;i<file_size/2;i++)
	{
		a = buf[2*i+1];
		buf[2*i+1] = buf[2*i];
		buf[2*i] = a;
	}
	outfile.Write(buf,file_size);
	free(buf);
	infile.Close();
	outfile.Close();
}
void CBinFileExDlg::OnButton1() 
{
	CFileDialog dlg(TRUE);	
	dlg.m_ofn.lpstrTitle = "打开要整理的文件";
	dlg.m_ofn.lpstrFilter = "BIN (*.bin)\0*.bin\0";    	
	if(dlg.DoModal()!=IDOK)
		return ;
	CString infilename = dlg.GetPathName();
	CString outfilename = infilename.Left(infilename.GetLength()-4)+"out.bin";
	
	CFile infile,outfile;
	infile.Open(infilename,CFile::modeRead| CFile::shareExclusive,NULL);
	outfile.Open(outfilename,CFile::modeWrite| CFile::modeCreate,NULL);
	int file_size = infile.GetLength();
	BYTE* buf;
	buf = (LPBYTE)malloc(sizeof(BYTE)*file_size);
	infile.Read(buf,file_size);
	BYTE a,b,c,d;
	for(int i=0;i<file_size/4;i++)
	{
		a = buf[4*i];
		b = buf[4*i+1];
		c = buf[4*i+2];
		d = buf[4*i+3];
		buf[4*i] = d;
		buf[4*i+1] = c;
		buf[4*i+2] = b;
		buf[4*i+3] = a;
	}
	outfile.Write(buf,file_size);
	free(buf);
	infile.Close();
	outfile.Close();
}
void CBinFileExDlg::OnConvert2() 
{
		
}
void CBinFileExDlg::OnConvert3() 
{
	FILE* ifp;							FILE* ofp;
	CString s1;							CString s2;
	GetDlgItemText(IDC_SOURCE_FILE,s1);	GetDlgItemText(IDC_DEST_FILE,s2);
	ifp = fopen(s1,"rb");				ofp = fopen(s2,"wb");
	
	int i;BYTE temp;
	char ch[1024];
	BYTE val[32];int j;
	while(!feof(ifp))
	{
		memset(ch,' ',1024);
		fgets(ch,1024,ifp);
		i = 0;j=0;
		do
		{
			if(ch[i]==0x0d)
				break;
			if(ch[i]==' '&&ch[i+1]!=' '&&ch[i+2]!=' '&&ch[i+3]==' ')
			{
				val[j] = hextoint(&ch[i+1],2);
				j++;
			}
			i++;			
		}while(i<65);
		fwrite(val,j,1,ofp);
	}
	fclose(ifp);
	fclose(ofp);
}


⌨️ 快捷键说明

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