日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

JAVA之CRC校验算法

發布時間:2023/12/31 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 JAVA之CRC校验算法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

用JAVA實現CRC校驗。

輸入byte型的數組,產生兩個元素的byte型數組(也就是16位)

public static byte[] makefcs(byte[] data){int crc=0xFFFF;byte[] buf = new byte[data.length];// 存儲需要產生校驗碼的數據 byte[] bup=new byte[2];for (int i = 0; i < data.length; i++) { buf[i] = data[i]; //數據的復制} int len = buf.length; for (int pos = 0; pos < len; pos++) { if (buf[pos] < 0) { crc ^= (int) buf[pos] + 256; //^異或:用于位運算,每個位相同為0,不同為1} else { crc ^= (int) buf[pos]; } for (int i = 8; i != 0; i--) { if ((crc & 0x0001) != 0) { crc >>= 1; //右移運算符crc ^= 0xA001; } else crc >>= 1;} } String c = Integer.toHexString(crc); if (c.length() == 4) { c = c.substring(2, 4) + c.substring(0, 2); } else if (c.length() == 3) { c = "0" + c; c = c.substring(2, 4) + c.substring(0, 2); } else if (c.length() == 2) { c = "0" + c.substring(1, 2) + "0" + c.substring(0, 1); }bup[0]=(byte)(Integer.parseInt(c.substring(0, 1), 16)+Integer.parseInt(c.substring(1,2), 16));bup[1]=(byte)(Integer.parseInt(c.substring(2, 3), 16)+Integer.parseInt(c.substring(3,4), 16));return bup; }

總結

以上是生活随笔為你收集整理的JAVA之CRC校验算法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。