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

📄 crc32dlg.cpp

📁 MD5程序
💻 CPP
字号:
// CRC32Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "CRC32.h"
#include "CRC32Dlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
DWORD crc32_table[256];
DWORD ulPolynomial = 0x04c11db7;
DWORD GenerateCRC32(char * DataBuf,DWORD len)
{
	DWORD oldcrc32;
	DWORD crc32;
	DWORD oldcrc;
	DWORD charcnt;
        char c,t;
	oldcrc32 = 0x00000000; //初值为0
    charcnt=0;
	while (len--) {
                t=(char)((oldcrc32 >> 24) & 0xFF); //要移出的字节的值
	oldcrc=crc32_table[t];         //根据移出的字节的值查表
                c=DataBuf[charcnt];    //新移进来的字节值
                oldcrc32= (oldcrc32 << 8) | c;   //将新移进来的字节值添在寄存器末字节中
                oldcrc32=oldcrc32^oldcrc;     //将寄存器与查出的值进行xor运算
                charcnt++;
	}
        crc32=oldcrc32;
        return crc32;
}
//参数表可以先在PC机上算出来,也可在程序初始化时完成。
//下面是用于计算参数表的c语言子程序,在Visual C++ 6.0下编译通过。
DWORD Reflect(DWORD ref, char ch)
{	DWORD value=0;
	// 交换bit0和bit7,bit1和bit6,类推
	for(int i = 1; i < (ch + 1); i++)
	{		if(ref & 1)
			value |= 1 << (ch - i);
	    	ref >>= 1;	}
	return value;
}

void init_crc32_table()
{	DWORD crc,temp,flag,t1,t2;
	// 256个值
	for(int i = 0; i <= 0xFF; i++)
	{   temp=Reflect(i, 8);
		crc32_table[i]= temp<< 24;
		for (int j = 0; j < 8; j++){
         flag=crc32_table[i]&0x80000000;
		 t1=(crc32_table[i] << 1);
		 if(flag==0) t2=0;
		 else t2=ulPolynomial;
		 crc32_table[i] =t1^t2 ;		}
		crc=crc32_table[i];
		crc32_table[i] = Reflect(crc32_table[i], 32);
	}
}

/////////////////////////////////////////////////////////////////////////////
// CCRC32Dlg dialog

CCRC32Dlg::CCRC32Dlg(CWnd* pParent /*=NULL*/)
	: CDialog(CCRC32Dlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CCRC32Dlg)
	m_Str = _T("12345678");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CCRC32Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CCRC32Dlg)
	DDX_Text(pDX, IDC_EDIT1, m_Str);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CCRC32Dlg, CDialog)
	//{{AFX_MSG_MAP(CCRC32Dlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_Compute, OnCompute)
	ON_EN_CHANGE(IDC_EDIT1, OnChangeEdit1)
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCRC32Dlg message handlers

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

	// 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
}

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

void CCRC32Dlg::OnCompute() 
{
init_crc32_table();
DWORD crc=GenerateCRC32((char*)(m_Str.GetBuffer(0)),m_Str.GetLength());

if(m_Str==""){
	AfxMessageBox("空字符串!");
	return ; }

char s[40];

sprintf(s,"src=%X",crc);
AfxMessageBox(s);

 


	
//return crc;



}

void CCRC32Dlg::OnChangeEdit1() 
{
UpdateData();
}

void CCRC32Dlg::OnButton2() 
{
DWORD x(0);;
char s[40];
sprintf(s,"%X",x);	
AfxMessageBox(s);
}

⌨️ 快捷键说明

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