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中调用程序

     

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

     

  • 相关阅读:
    3、tensorflow变量运算,数学运算
    2、tensorflow 变量的初始化
    1、tensorflow 框架理解
    tensorflow 打印全部变量的一种方法
    0、tensorflow学习开始
    tensorflow 小记——如何对张量做任意行求和,得到新tensor(一种方法:列表生成式)
    SASRec 实践
    jupyterlab 增加新内核的方法ipykernel
    vivo 全球商城:架构演进之路
    jenkins安装 git免密ssh配置
  • 原文地址:https://www.cnblogs.com/Dukechris/p/4396553.html
Copyright © 2011-2022 走看看