zoukankan      html  css  js  c++  java
  • 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中调用程序

     

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

     

  • 相关阅读:
    SpringBoot自定义FailureAnalyzer
    Linux 系统目录结构
    Spring Boot实战:静态资源处理
    Activiti7整合SpringBoot(十二)
    Acvitivi网关(十一)
    为什么阿里巴巴要禁用Executors创建线程池?
    lqb 基础练习 数列排序 (sort的使用)
    lqb 入门训练 A+B问题
    lqb 入门训练 序列求和 (PS:用长整数做数据的输入输出)
    lqb 入门训练 圆的面积 (PS: PI的精确计算方法 atan(1.0) * 4)
  • 原文地址:https://www.cnblogs.com/Dukechris/p/4396553.html
Copyright © 2011-2022 走看看