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

📄 ccittdlg.cpp

📁 能够对十六进制的数据进行CRC校验
💻 CPP
字号:
// ccittDlg.cpp : implementation file
//

#include "stdafx.h"
#include "ccitt.h"
#include "ccittDlg.h"

#include "stdio.h"
#include <stdlib.h>
#include <string.h>

#define MAX_STRING_SIZE 1024
#define P_CCITT 0x1021

#ifdef _DEBUG
#define new DEBUG_NEW

#define FALSE           0
#define TRUE            1
#define                 P_CCITT     0x1021
#define MAX_STRING_SIZE    2048

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

/////////////////////////////////////////////////////////////////////////////
// CCcittDlg dialog

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

void CCcittDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CCcittDlg)
	DDX_Text(pDX, IDC_EDIT1, m_edit1);
	DDV_MaxChars(pDX, m_edit1, 2048);
	DDX_Text(pDX, IDC_EDIT2, m_edit2);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CCcittDlg, CDialog)
	//{{AFX_MSG_MAP(CCcittDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCcittDlg message handlers

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

unsigned short          update_crc_ccitt( unsigned short crc, char c );
static int              crc_tabccitt_init       = FALSE;
static unsigned short   crc_tabccitt[256];

static void             init_crcccitt_tab( void );

static void init_crcccitt_tab( void ) 
{

    int i, j;
    unsigned short crc, c;

    for (i=0; i<256; i++) {

        crc = 0;
        c   = ((unsigned short) i) << 8;

        for (j=0; j<8; j++) {

            if ( (crc ^ c) & 0x8000 ) crc = ( crc << 1 ) ^ P_CCITT;
            else                      crc =   crc << 1;

            c = c << 1;
        }

        crc_tabccitt[i] = crc;
    }

    crc_tabccitt_init = TRUE;

}  /* init_crcccitt_tab */


unsigned short update_crc_ccitt( unsigned short crc, char c ) {

    unsigned short tmp, short_c;

    short_c  = 0x00ff & (unsigned short) c;

    if ( ! crc_tabccitt_init ) init_crcccitt_tab();

    tmp = (crc >> 8) ^ short_c;
    crc = (crc << 8) ^ crc_tabccitt[tmp];

    return crc;

}  /* update_crc_ccitt */




/*void crc_main( char input_string[MAX_STRING_SIZE])
   {

    char input_string[MAX_STRING_SIZE];
    char *ptr, *dest, hex_val;
    unsigned short crc_ccitt_ffff, low_byte, high_byte,a1,b;
    unsigned long crc_32;
    int a, ch;  
        
	ptr  = input_string;
    dest = input_string;

    while( *ptr  &&  *ptr != '\r'  &&  *ptr != '\n' )
          {

            if ( *ptr >= '0'  &&  *ptr <= '9' ) *dest++ = (char) ( (*ptr) - '0'      );
            if ( *ptr >= 'A'  &&  *ptr <= 'F' ) *dest++ = (char) ( (*ptr) - 'A' + 10 );
            if ( *ptr >= 'a'  &&  *ptr <= 'f' ) *dest++ = (char) ( (*ptr) - 'a' + 10 );

            ptr++;
           }

        * dest    = '\x80';
        *(dest+1) = '\x80'; 

    a = 1;

    do {
         crc_ccitt_ffff = 0xffff;
         ptr = input_string;
         while ( *ptr != '\x80' )
              {

                hex_val  = (char) ( ( * ptr     &  '\x0f' ) << 4 );
                hex_val |= (char) ( ( *(ptr+1)  &  '\x0f' )      );
                crc_ccitt_ffff = update_crc_ccitt( crc_ccitt_ffff, hex_val );
                ptr += 2;
              }

            input_string[0] = 0;
          

        crc_ccitt_ffff = ~crc_ccitt_ffff;


        a1 = crc_ccitt_ffff <<8;
        b = crc_ccitt_ffff>>8;

        crc_ccitt_ffff= a1^b;
	}
}  /* main (tst_crc.c) */



void CCcittDlg::OnButton1() 
{
	// TODO: Add your control notification handler code here
    
	char input_string[MAX_STRING_SIZE];
	int outdata[4];
    char *ptr, *dest, hex_val;
    unsigned short crc_ccitt_ffff,a1,b;   
    int i,j;  
    
	CString str_input,put_string,s;
	CString	input_string1[4];
	UpdateData(TRUE);
    put_string=m_edit1;
    str_input="";
	//ptr  = input_string;
    //dest = input_string;
    j=strlen(put_string);
    for (i=0;i<j;i++) 
	{
		if (put_string.Mid(i,1)!=' ') str_input=str_input+put_string.Mid(i,1);
	}
    j=strlen(str_input);

    for(i=0;i<j;i++) 
	{
	    if (str_input.Mid(i,1)=="0") input_string[i]='0';
		if (str_input.Mid(i,1)=="1") input_string[i]='1';
		if (str_input.Mid(i,1)=="2") input_string[i]='2';
		if (str_input.Mid(i,1)=="3") input_string[i]='3';
		if (str_input.Mid(i,1)=="4") input_string[i]='4';
		if (str_input.Mid(i,1)=="5") input_string[i]='5';
		if (str_input.Mid(i,1)=="6") input_string[i]='6';
		if (str_input.Mid(i,1)=="7") input_string[i]='7';
		if (str_input.Mid(i,1)=="8") input_string[i]='8';
		if (str_input.Mid(i,1)=="9") input_string[i]='9';
		if (str_input.Mid(i,1)=="a" || str_input.Mid(i,1)=="A") input_string[i]='A';
		if (str_input.Mid(i,1)=="b" || str_input.Mid(i,1)=="B") input_string[i]='B';
		if (str_input.Mid(i,1)=="c" || str_input.Mid(i,1)=="C") input_string[i]='C';
		if (str_input.Mid(i,1)=="d" || str_input.Mid(i,1)=="D") input_string[i]='D';
		if (str_input.Mid(i,1)=="e" || str_input.Mid(i,1)=="E") input_string[i]='E';
		if (str_input.Mid(i,1)=="f" || str_input.Mid(i,1)=="F") input_string[i]='F';

	}
    ptr  = input_string;
    dest = input_string;
   while( *ptr  &&  *ptr != '\r'  &&  *ptr != '\n' )
          {

            if ( *ptr >= '0'  &&  *ptr <= '9' ) *dest++ = (char) ( (*ptr) - '0'      );
            if ( *ptr >= 'A'  &&  *ptr <= 'F' ) *dest++ = (char) ( (*ptr) - 'A' + 10 );
            if ( *ptr >= 'a'  &&  *ptr <= 'f' ) *dest++ = (char) ( (*ptr) - 'a' + 10 );

            ptr++;
           }

        * dest    = '\x80';
        *(dest+1) = '\x80'; 

    
         crc_ccitt_ffff = 0xffff;
         ptr = input_string;
         while ( *ptr != '\x80' && *ptr != ' ')
              {

                hex_val  = (char) ( ( * ptr     &  '\x0f' ) << 4 );
                hex_val |= (char) ( ( *(ptr+1)  &  '\x0f' )      );
                crc_ccitt_ffff = update_crc_ccitt( crc_ccitt_ffff, hex_val );
                ptr += 2;
              }

            input_string[0] = 0;
            crc_ccitt_ffff = ~crc_ccitt_ffff;
            a1 = crc_ccitt_ffff <<8;
            b = crc_ccitt_ffff>>8;
            crc_ccitt_ffff= a1^b;
            //m_edit2= crc_ccitt_ffff;
			//UpdateData(FALSE);
	for(i=3;i>=0;i--)
	  {
         outdata[i]=crc_ccitt_ffff % 16;
         crc_ccitt_ffff=crc_ccitt_ffff / 16;         
	  }
    
	   for(i=0;i<4;i++) 
	{
	    if (outdata[i]==0) input_string1[i]="0";
		if (outdata[i]==1) input_string1[i]="1";
		if (outdata[i]==2) input_string1[i]="2";
		if (outdata[i]==3) input_string1[i]="3";
		if (outdata[i]==4) input_string1[i]="4";
		if (outdata[i]==5) input_string1[i]="5";
		if (outdata[i]==6) input_string1[i]="6";
		if (outdata[i]==7) input_string1[i]="7";
		if (outdata[i]==8) input_string1[i]="8";
		if (outdata[i]==9) input_string1[i]="9";
		if (outdata[i]==10) input_string1[i]="A";
		if (outdata[i]==11) input_string1[i]="B";
		if (outdata[i]==12) input_string1[i]="C";
		if (outdata[i]==13) input_string1[i]="D";
		if (outdata[i]==14) input_string1[i]="E";
		if (outdata[i]==15) input_string1[i]="F";
		
	}
  
 for(i=0;i<4;i++)
 {
    s=s+input_string1[i];
 }
 m_edit2="0x"+s;
 UpdateData(FALSE);
}

void CCcittDlg::OnButton2() 
{
	// TODO: Add your control notification handler code here
	m_edit1="";
    m_edit2="";
    UpdateData(FALSE);
	
}

⌨️ 快捷键说明

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