標籤:
這次的部落格記錄的是寫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