CRC校验程序2:通过命令提示符加载文本文档作为数据源进行CRC校验,输出校验码

        将CRC校验函数封装在类中,通过控制台传参(文件的相对路径),进行CRC校验。

 1 using System;
 2 using System.Collections.Generic;
 3 using System.IO;
 4 using System.Linq;
 5 using System.Text;
 6 using System.Threading.Tasks;
 7 
 8 namespace CRC
 9 {
10     class Program
11     {
12         static void Main(string[] args)
13         {
14             UInt32 LastCRC = 0x0000;
15             try
16             {
17                 FileStream FileBytes = new FileStream(args[0], FileMode.Open, FileAccess.Read);
18                 BinaryReader BinaryFile = new BinaryReader(FileBytes);
19                 while (true)
20                 {
21                     LastCRC = CalaCRC.CRCCalculating(BinaryFile.ReadByte());
22                 }
23                 FileBytes.Close();
24             }
25             catch (EndOfStreamException o)
26             {
27                 LastCRC = CalaCRC.CRCUpside(LastCRC);
28             }
29             Console.WriteLine(LastCRC);
30         }
31     }
32     class CalaCRC
33     {
34         public static UInt32 CRCCalculating(UInt32 CRCByte){
35             UInt32 CRCRegister = 0xFFFF;
36             CRCRegister = (CRCByte ^ CRCRegister);
37             CRCRegister = CRCRegister >> 1;
38             int i = 1;
39 
40             for (; i <= 8; i++)
41             {
42                 if (CRCRegister % 2 == 0)
43                 {
44                     CRCRegister = CRCRegister ^ 0xA001;
45                     i--;
46                 }
47                 else
48                     CRCRegister = CRCRegister >> 1;
49             }
50             return CRCRegister;
51         }
52 
53         public static UInt32 CRCUpside(UInt32 CRCRegister)
54         {
55             UInt32 CrcData = CRCRegister % 256;
56             CRCRegister = CRCRegister / 256;
57             CrcData = CrcData * 256;
58             CRCRegister = CRCRegister + CrcData;
59             return CRCRegister;
60         }
61     }
62 }

使用FileStream类读取文件,外部传参进去得到校验文件的相对路径,同时输出结果。

 

在Test.txt中有测试用的16进制数据原型:

 

在console中调用程序

 

下一篇,用窗体程序调用校验检查算法。

 

posted @ 2015-04-06 20:44  Mr.Dukechris  阅读(491)  评论(0编辑  收藏  举报