我的C#入門之路_Day4

來源:互聯網
上載者:User

標籤:

這次的部落格記錄的是寫crc校正碼的過程。

過程十分坎坷,好不容易快整完了結果虛擬機器崩了,程式也沒了,只好重新來過。。。

首先是控制台應用程式的代碼。

 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using System.IO; 7  8  9 namespace ConsoleApplication110 {11     class Program12     {13         static Int64 a(int length, byte[] data)14         {15             Int64 CRCtemp = 65535;16             int j = 0;17             int chr = 0;18             int chr1 = 0;19             for (int y = 0; y < length; y++)20             {21                 chr = (int)CRCtemp & 255;22                 chr = chr ^ data[j];23                 CRCtemp = CRCtemp & 0xff00;24                 CRCtemp = CRCtemp + chr;25                 for (int i = 0; i < 8; i++)26                 {27                     if ((CRCtemp & 0x01) == 1)28                     {29                         CRCtemp = CRCtemp >> 1;30                         CRCtemp = CRCtemp ^ 0xA001;31 32                     }33                     else34                     {35                         CRCtemp = CRCtemp >> 1;36                     }37                 }38                 j += 1;39             }40             chr = (int)CRCtemp & 0xff;41             chr1 = (int)CRCtemp & 0xff00;42             CRCtemp = chr << 8 | chr1 >> 8;43             return CRCtemp;44         }45         static void Main(string[] args)46         {47             string str;48             Console.WriteLine("input your file please:");49             str = Console.ReadLine();50             FileStream fs = new FileStream(str, FileMode.Open);51             byte[] bt = new byte[1000];52             int i = 0;53             BinaryReader br = new BinaryReader(fs);54             while(br.PeekChar() >= 0)55             {56                 bt[i] = br.ReadByte();57                 i++;58             }59             Int64 result;60             result = a(i, bt);61             Console.WriteLine("the result is 0x" + result);62             63             64 65         }66     }67 }

其中的演算法是讀了許久的實驗要求,又百度了好久才看懂的。。。

然後是測試檔案“123.txt”的內容:

crccalculation

最後是運行結果:

(ps:字可能有些小了)

我的C#入門之路_Day4

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.