📄 crctestdlg.cpp
字号:
// CRCTestDlg.cpp : implementation file
//
#include "stdafx.h"
#include "CRCTest.h"
#include "CRCTestDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CCRCTestDlg dialog
CCRCTestDlg::CCRCTestDlg(CWnd* pParent /*=NULL*/)
: CDialog(CCRCTestDlg::IDD, pParent)
{
m_strDataFile = _T("");
//{{AFX_DATA_INIT(CCRCTestDlg)
m_nCRCMode = 2;
m_crcInitValue = 0;
m_bNotValue = FALSE;
m_strData = _T("");
m_strCRC = _T("");
m_bDataNot = FALSE;
m_nCRCMethod = 1;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CCRCTestDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CCRCTestDlg)
DDX_Control(pDX, IDC1_R_CRC_CUSTOM, m_radCRCCustom);
DDX_Radio(pDX, IDC1_R_CRC_12, m_nCRCMode);
DDX_Radio(pDX, IDC1_R_CRC_INITIAL_VALUE_0, m_crcInitValue);
DDX_Check(pDX, IDC1_CK_CRC_NOT, m_bNotValue);
DDX_Text(pDX, IDC1_E_DATA, m_strData);
DDX_Text(pDX, IDC1_E_CRC_RESULT, m_strCRC);
DDX_Check(pDX, IDC1_CK_DATA_NOT, m_bDataNot);
DDX_Radio(pDX, IDC1_R_CRC_METHOD_16, m_nCRCMethod);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CCRCTestDlg, CDialog)
//{{AFX_MSG_MAP(CCRCTestDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC1_B_CRC, OnBCrc)
ON_BN_CLICKED(IDC1_R_CRC_12, OnRCrc)
ON_BN_CLICKED(IDC1_R_CRC_CUSTOM, OnRCrcCustom)
ON_BN_CLICKED(IDC1_CK_CRC_NOT, OnCkCrcNot)
ON_BN_CLICKED(IDC1_B_DATA_FILE, OnBDataFile)
ON_BN_CLICKED(IDC1_R_CRC_16, OnRCrc)
ON_BN_CLICKED(IDC1_R_CRC_32, OnRCrc)
ON_BN_CLICKED(IDC1_R_CRC_CCITT, OnRCrc)
ON_BN_CLICKED(IDC1_CK_DATA_CLEAR, OnCkDataClear)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CCRCTestDlg message handlers
BOOL CCRCTestDlg::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
SubClassComboBox(IDC1_LST_CRC_POLY);
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 CCRCTestDlg::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 CCRCTestDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
#include "CRCTable.h"
void CCRCTestDlg::OnBCrc()
{
UpdateData();
BOOL X[33];
memset(X,0,sizeof(X));
CString str,str1;
str="CRC-自定义: ";
if(m_nCRCMode==0)//CRC12
{
X[12]=1;
X[11]=1;
X[ 3]=1;
X[ 2]=1;
X[ 1]=1;
X[ 0]=1;
}
else if(m_nCRCMode==1)//CRC16
{
X[16]=1;
X[15]=1;
X[ 2]=1;
X[ 0]=1;
}
else if(m_nCRCMode==2)//CRC-CCITT
{
X[16]=1;
X[12]=1;
X[ 5]=1;
X[ 0]=1;
}
else if(m_nCRCMode==3)//CRC32
{
//X32+x26+x23+x22+x16+x12+x11+x10+x8+x7+x5+x4+x2+x+x0
X[32]=1;
X[26]=1;
X[23]=1;
X[22]=1;
X[16]=1;
X[12]=1;
X[11]=1;
X[10]=1;
X[ 8]=1;
X[ 7]=1;
X[ 5]=1;
X[ 4]=1;
X[ 2]=1;
X[ 1]=1;
X[ 0]=1;
}
else if(m_nCRCMode==4)
{
str="";
for(int i=0;i<33;i++)
{
X[i]=m_chkLst.GetCheck(i);
if(X[i])
{
str1.Format("+x%d",i);
str=str1+str;
}
}
if(str.GetLength()>0) str=str.Mid(1);
str="CRC-自定义: "+str;
}
m_radCRCCustom.SetWindowText(str);
if(m_strDataFile.GetLength()==0)
{
CString strBuf,strData;
strData=m_strData;
strData.TrimLeft();
strData.TrimRight();
strData.MakeLower();
strData.Replace("\t","");
strData.Replace("\r","");
strData.Replace("\n","");
m_strData = strData;
if(m_strData.GetLength()==0)
{
m_nCRCMethod?CCRCTable256(X,33).ShowCRCTable(&m_strData):
CCRCTable16(X,33).ShowCRCTable(&m_strData);
UpdateData(FALSE);
return;
}
strData.Replace(",","");
strData.Replace(" ","");
strData.Replace("0x","");
if(strData.GetLength()==0){return ;}
if(strData.GetLength()&1){MessageBox("数据长度应该是偶数"); return ;}
CHAR*p=strBuf.GetBuffer(strData.GetLength()/2);
CHAR h;
for(int k=0;k<strData.GetLength()/2;k++)
{
h=strData.GetAt(k<<1);
if(h>='a')h-='a'-0xa;
else h-='0';
if((h<0)||(h>15)) {MessageBox("含有非法数据"); return ;}
p[k]=(h<<4);
h=strData.GetAt((k<<1)+1);
if(h>='a')h-='a'-0xa;
else h-='0';
if((h<0)||(h>15)) {MessageBox("含有非法数据"); return ;}
p[k]|=h&0x0f;
if(m_bDataNot)p[k]=~p[k];
}
DWORD m_dwCRC=
m_nCRCMethod?
CCRCTable256(X,33).CRC((BYTE*)p,k,~(m_crcInitValue-1)):
CCRCTable16(X,33).CRC((BYTE*)p,k,~(m_crcInitValue-1));
strBuf.ReleaseBuffer();
if(m_bNotValue)m_dwCRC=~m_dwCRC;
DWORD i=CCRCTable16(X,33).m_bits;
if(i!=32)m_dwCRC&=((1<<i)-1);
m_strCRC.Format("0X%08X",m_dwCRC);
}
else
{
m_strData="";
CStdioFile f;
if(!f.Open(m_strDataFile,CFile::modeRead|CFile::shareDenyNone))
{
AfxMessageBox(m_strDataFile+"文件不存在!");
return;
}
CString str,strID,strBuf,strData;
U32 crcBytes=0;
while(f.ReadString(str)){
if(1==::sscanf(str,"校验数据字节数:%d",&crcBytes))
f.ReadString(str);
break;
}
do{
strData=str;
strData.TrimLeft();
strData.TrimRight();
strData.MakeLower();
strData.Replace("\t","");
strData.Replace("\r","");
strData.Replace("\n","");
strData.Replace(",","");
strData.Replace(" ","");
strData.Replace("0x","");
int iFind=strData.Find(":");
if(iFind>0)
{
strID=strData.Left(iFind+1);
strData=strData.Mid(iFind+1);
}
else strID="";
if(strData.GetLength()==0){ continue; }
if(strData.GetLength()&1){MessageBox(str+"\n数据长度应该是偶数"); return ;}
if(strData.GetLength()<(crcBytes+1)*2){MessageBox(str+"\n数据中应包含校验数据"); return ;}
BYTE*p=(BYTE*)strBuf.GetBuffer(strData.GetLength()/2);
BYTE h;
for(int k=0;k<strData.GetLength()/2;k++)
{
h=strData.GetAt(k<<1);
if(h>='a')h-='a'-0xa;
else h-='0';
if((h<0)||(h>15)) {MessageBox("含有非法数据"); return ;}
p[k]=(h<<4);
h=strData.GetAt((k<<1)+1);
if(h>='a')h-='a'-0xa;
else h-='0';
if((h<0)||(h>15)) {MessageBox("含有非法数据"); return ;}
p[k]|=h&0x0f;
if(m_bDataNot)p[k]=~p[k];
}
DWORD m_dwCRC=
m_nCRCMethod?
CCRCTable256(X,33).CRC((BYTE*)p,k-crcBytes,~(m_crcInitValue-1)):
CCRCTable16(X,33).CRC((BYTE*)p,k-crcBytes,~(m_crcInitValue-1));
strBuf.ReleaseBuffer();
if(m_bNotValue)m_dwCRC=~m_dwCRC;
DWORD i=CCRCTable16(X,33).m_bits;
if(i!=32)m_dwCRC&=((1<<i)-1);
str.Format("0X%08X\r\n",m_dwCRC);
m_strData+=strID+str;
}while(f.ReadString(str));
}
UpdateData(FALSE);
}
void CCRCTestDlg::OnCkDataClear()
{
// TODO: Add your control notification handler code here
m_strData="";
UpdateData(FALSE);
}
void CCRCTestDlg::OnRCrc()
{
// TODO: Add your control notification handler code here
m_radCRCCustom.SetWindowText("CRC-自定义: ");
m_chkLst.EnableWindow(FALSE);
}
void CCRCTestDlg::OnRCrcCustom()
{
// TODO: Add your control notification handler code here
OnRCrc();
m_chkLst.EnableWindow(TRUE);
}
void CCRCTestDlg::OnCkCrcNot()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
//m_bNotValue=!m_bNotValue;
DWORD m_dwCRC=0;
sscanf(m_strCRC,"0X%X",&m_dwCRC);
m_dwCRC=~m_dwCRC;
m_strCRC.Format("0X%08X",m_dwCRC);
UpdateData(FALSE);
}
void CCRCTestDlg::OnCancel()
{
// TODO: Add extra cleanup here
YM_SY();
CDialog::OnCancel();
}
VOID SetCurDir()
{
CHAR curPath[255];
GetModuleFileName(NULL,curPath,255);
*strrchr(curPath,'\\')='\0';
BOOL b=SetCurrentDirectory(curPath);
}
void CCRCTestDlg::OnBDataFile()
{
// TODO: Add your control notification handler code here
CHAR curPath[255]={0};
GetModuleFileName(NULL,curPath,255);
*(strrchr(curPath,'\\')+1)='\0';
//strcat(curPath,"*.txt");
CFileDialog f(TRUE);
//SetCurDir();
//static CHAR* sFilter="*.txt\0*.txt\0所有文件(*.*)\0*.*\0\0";
//f.m_ofn.lpstrFile=curPath;
f.m_ofn.lpstrInitialDir=curPath;
f.m_ofn.lpstrFilter="*.txt\0*.txt\0所有文件(*.*)\0*.*\0\0";
if(f.DoModal()==IDOK)
{
m_strDataFile=f.GetFileName();
OnBCrc();
m_strDataFile="";
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -