VB CRC校驗演算法實現

2021-05-01 10:19:38 字數 3849 閱讀 6512

以下是crc16位 校驗函式,使用的是查表法

private function crc16(data() as byte) as string

dim crc16hi as byte

dim crc16lo as byte

crc16hi = &hff

crc16lo = &hff

dim i as integer

dim iindex as long

for i = 0 to ubound(data)

iindex = crc16lo xor data(i)

crc16lo = crc16hi xor getcrclo(iindex)        '低位處理

crc16hi = getcrchi(iindex)                    '高位處理

next i

dim returndata(1) as byte

returndata(0) = crc16hi        'crc高位

returndata(1) = crc16lo        'crc低位

crc16 = returndata

end function

'crc低位位元組值表

function getcrclo(ind as long) as byte

getcrclo = choose(ind + 1, &h0, &hc1, &h81, &h40, &h1, &hc0, &h80, &h41, &h1, &hc0, &h80, &h41, &h0, &hc1, &h81, &h40, &h1, &hc0, &h80, &h41, &h0, &hc1, &h81, &h40, &h0, &hc1, &h81, &h40, &h1, &hc0, &h80, &h41, &h1, &hc0, &h80, &h41, &h0, &hc1, &h81, &h40, &h0, &hc1, &h81, &h40, &h1, &hc0, &h80, &h41, &h0, &hc1, &h81, &h40, &h1, &hc0, &h80, &h41, &h1, &hc0, &h80, &h41, &h0, &hc1, &h81, &h40, &h1, &hc0, &h80, &h41, &h0, &hc1, &h81, &h40, &h0, &hc1, &h81, &h40, &h1, &hc0, &h80, &h41, &h0, &hc1, &h81, &h40, &h1, &hc0, &h80, &h41, &h1, &hc0, &h80, &h41, &h0, &hc1, &h81, &h40, &h0, &hc1, &h81, &h40, &h1, &hc0, &h80, &h41, &h1, &hc0, &h80, &h41, &h0, &hc1, &h81, &h40, &h1, &hc0, &h80, &h41, &h0, &hc1, &h81, &h40, &h0, &hc1, &h81, &h40, &h1, &hc0, &h80, &h41, &h1, &hc0, _

&h80, &h41, &h0, &hc1, &h81, &h40, &h0, &hc1, &h81, &h40, &h1, &hc0, &h80, &h41, &h0, &hc1, &h81, &h40, &h1, &hc0, &h80, &h41, &h1, &hc0, &h80, &h41, &h0, &hc1, &h81, &h40, &h0, &hc1, &h81, &h40, &h1, &hc0, &h80, &h41, &h1, &hc0, &h80, &h41, &h0, &hc1, &h81, &h40, &h1, &hc0, &h80, &h41, &h0, &hc1, &h81, &h40, &h0, &hc1, &h81, &h40, &h1, &hc0, &h80, &h41, &h0, &hc1, &h81, &h40, &h1, &hc0, &h80, &h41, &h1, &hc0, &h80, &h41, &h0, &hc1, &h81, &h40, &h1, &hc0, &h80, &h41, &h0, &hc1, &h81, &h40, &h0, &hc1, &h81, &h40, &h1, &hc0, &h80, &h41, &h1, &hc0, &h80, &h41, &h0, &hc1, &h81, &h40, &h0, &hc1, &h81, &h40, &h1, &hc0, &h80, &h41, &h0, &hc1, &h81, &h40, &h1, &hc0, &h80, &h41, &h1, &hc0, &h80, &h41, &h0, &hc1, &h81, &h40)

end function

'crc高位位元組值表

function getcrchi(ind as long) as byte

getcrchi = choose(ind + 1, &h0, &hc0, &hc1, &h1, &hc3, &h3, &h2, &hc2, &hc6, &h6, &h7, &hc7, &h5, &hc5, &hc4, &h4, &hcc, &hc, &hd, &hcd, &hf, &hcf, &hce, &he, &ha, &hca, &hcb, &hb, &hc9, &h9, &h8, &hc8, &hd8, &h18, &h19, &hd9, &h1b, &hdb, &hda, &h1a, &h1e, &hde, &hdf, &h1f, &hdd, &h1d, &h1c, &hdc, &h14, &hd4, &hd5, &h15, &hd7, &h17, &h16, &hd6, &hd2, &h12, &h13, &hd3, &h11, &hd1, &hd0, &h10, &hf0, &h30, &h31, &hf1, &h33, &hf3, &hf2, &h32, &h36, &hf6, &hf7, &h37, &hf5, &h35, &h34, &hf4, &h3c, &hfc, &hfd, &h3d, &hff, &h3f, &h3e, &hfe, &hfa, &h3a, &h3b, &hfb, &h39, &hf9, &hf8, &h38, &h28, &he8, &he9, &h29, &heb, &h2b, &h2a, &hea, &hee, &h2e, &h2f, &hef, &h2d, &hed, &hec, &h2c, &he4, &h24, &h25, &he5, &h27, &he7, &he6, &h26, &h22, &he2, &he3, &h23, &he1, &h21, &h20, &he0, &ha0, &h60, _

&h61, &ha1, &h63, &ha3, &ha2, &h62, &h66, &ha6, &ha7, &h67, &ha5, &h65, &h64, &ha4, &h6c, &hac, &had, &h6d, &haf, &h6f, &h6e, &hae, &haa, &h6a, &h6b, &hab, &h69, &ha9, &ha8, &h68, &h78, &hb8, &hb9, &h79, &hbb, &h7b, &h7a, &hba, &hbe, &h7e, &h7f, &hbf, &h7d, &hbd, &hbc, &h7c, &hb4, &h74, &h75, &hb5, &h77, &hb7, &hb6, &h76, &h72, &hb2, &hb3, &h73, &hb1, &h71, &h70, &hb0, &h50, &h90, &h91, &h51, &h93, &h53, &h52, &h92, &h96, &h56, &h57, &h97, &h55, &h95, &h94, &h54, &h9c, &h5c, &h5d, &h9d, &h5f, &h9f, &h9e, &h5e, &h5a, &h9a, &h9b, &h5b, &h99, &h59, &h58, &h98, &h88, &h48, &h49, &h89, &h4b, &h8b, &h8a, &h4a, &h4e, &h8e, &h8f, &h4f, &h8d, &h4d, &h4c, &h8c, &h44, &h84, &h85, &h45, &h87, &h47, &h46, &h86, &h82, &h42, &h43, &h83, &h41, &h81, &h80, &h40)

end function

C 實現CRC校驗演算法

2007 06 16 10 06 by l,2734 visits,收藏,編輯 region crc校驗 crc高位校驗碼checkcrchigh static byte arraycrchigh crc地位校驗碼checkcrclow static byte checkcrclow crc校驗 校...

TCP UDP 校驗演算法

經常看計算機網路相關的書時,每次看到關於ip或者是udp報頭校驗和時,都是一笑而過,以為相當簡單的東西,不就是16bit資料的相加嗎!最近在學習ping命令的源待時,看到裡面有關於校驗和的演算法。一頭霧水,後來查詢資料,看到校驗和是16bit字的二進位制反碼和。總是覺得很奇怪,為什麼會用反碼和,而不...

CRC16校驗演算法實現

迴圈冗餘碼校驗英文名稱為cyclical redundancy check,簡稱crc。它是利用除法及餘數的原理來作錯誤偵測 error detecting 的。實際應用時,傳送裝置計算出crc值並隨資料一同傳送給接收裝置,接收裝置對收到的資料重新計算crc並與收到的crc相比較,若兩個crc值不同...