由于res定義為unsigned int,所以包含4個(gè)字節(jié);而p是unsigned char的指針,指向的數(shù)據(jù)是1個(gè)字節(jié)。

成都創(chuàng)新互聯(lián)專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于網(wǎng)站設(shè)計(jì)、網(wǎng)站制作、黃岡網(wǎng)絡(luò)推廣、成都小程序開發(fā)、黃岡網(wǎng)絡(luò)營銷、黃岡企業(yè)策劃、黃岡品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運(yùn)營等,從售前售中售后,我們都將竭誠為您服務(wù),您的肯定,是我們最大的嘉獎(jiǎng);成都創(chuàng)新互聯(lián)為所有大學(xué)生創(chuàng)業(yè)者提供黃岡建站搭建服務(wù),24小時(shí)服務(wù)熱線:18980820575,官方網(wǎng)址:chinadenli.net
所以常規(guī)來說,假如res由byte3 byte2 byte1 byte0構(gòu)成,那么p[0]=byte0,p[1]=byte1,p[2]=byte2,p[3]=byte4,也就是p[0]是最低字節(jié),p[3]是最高字節(jié)。
但是還要看系統(tǒng)硬件連接,是big endian還是 little endian,如果是 little endian那么就是常規(guī)情況,和上面一樣。如果是big endian,那么數(shù)據(jù)存放是倒過來的,也就是p[0]是最高字節(jié),p[3]是最低字節(jié),全部情況是p[0]=byte3,p[1]=byte2,p[2]=byte1,p[3]=byte0。
代碼如下
public static byte CRC8(byte[] buffer)
? {
? ? ? byte crc = 0;
? ? ? for (int j = 0; j buffer.Length; j++)
? ? ? {
? ? ? ? ? crc ^= buffer[j];
? ? ? ? ? for (int i = 0; i 8; i++)
? ? ? ? ? {
? ? ? ? ? ? ? if ((crc 0x01) != 0)
? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? crc = 1;
? ? ? ? ? ? ? ? ? crc ^= 0x8c;
? ? ? ? ? ? ? }
? ? ? ? ? ? ? else
? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? crc = 1;
? ? ? ? ? ? ? }
? ? ? ? ? }
? ? ? }
? ? ? return crc;
? }
在線生成工具
這個(gè)生成的是function,不可綜合的。自己改成module就行了
////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 1999-2008 Easics NV.
// This source file may be used and distributed without restriction
// provided that this copyright statement is not removed from the file
// and that any derivative work contains the original copyright notice
// and the associated disclaimer.
//
// THIS SOURCE FILE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS
// OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
// WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
//
// Purpose : synthesizable CRC function
// * polynomial: (0 3 4 5 8)
// * data width: 8
//
// Info : tools@easics.be
//
////////////////////////////////////////////////////////////////////////////////
module CRC8_D8;
// polynomial: (0 3 4 5 8)
// data width: 8
// convention: the first serial bit is D[7]
function [7:0] nextCRC8_D8;
input [7:0] Data;
input [7:0] crc;
reg [7:0] d;
reg [7:0] c;
reg [7:0] newcrc;
begin
d = Data;
c = crc;
newcrc[0] = d[6] ^ d[5] ^ d[4] ^ d[3] ^ d[0] ^ c[0] ^ c[3] ^ c[4] ^ c[5] ^ c[6];
newcrc[1] = d[7] ^ d[6] ^ d[5] ^ d[4] ^ d[1] ^ c[1] ^ c[4] ^ c[5] ^ c[6] ^ c[7];
newcrc[2] = d[7] ^ d[6] ^ d[5] ^ d[2] ^ c[2] ^ c[5] ^ c[6] ^ c[7];
newcrc[3] = d[7] ^ d[5] ^ d[4] ^ d[0] ^ c[0] ^ c[4] ^ c[5] ^ c[7];
newcrc[4] = d[4] ^ d[3] ^ d[1] ^ d[0] ^ c[0] ^ c[1] ^ c[3] ^ c[4];
newcrc[5] = d[6] ^ d[3] ^ d[2] ^ d[1] ^ d[0] ^ c[0] ^ c[1] ^ c[2] ^ c[3] ^ c[6];
newcrc[6] = d[7] ^ d[4] ^ d[3] ^ d[2] ^ d[1] ^ c[1] ^ c[2] ^ c[3] ^ c[4] ^ c[7];
newcrc[7] = d[5] ^ d[4] ^ d[3] ^ d[2] ^ c[2] ^ c[3] ^ c[4] ^ c[5];
nextCRC8_D8 = newcrc;
end
endfunction
endmodule
以下是我的分析,不知是否正確,你參考下1、首先來看你打java代碼:crc=(byte)((crc1)^0x8c);和 crc=(byte)(crc1); 導(dǎo)致這個(gè)問題是因?yàn)閎yte的最高位符號位,轉(zhuǎn)換的時(shí)候就出錯(cuò)了2、示例代碼:package com.test;public class test {public static void main(String[] args) {byte[] ptr = { 1, 1, 1, 1, 1, 1 };byte res = getCrc(ptr);System.out.println();System.out.println((byte)( (1 1) ^ 0x8c ) + ":" +( (1 1) ^ 0x8c ) );}public static byte getCrc(byte[] ptr) {int crc = 0;for (int i = 0; i 1) ^ 0x8c;} else {crc = crc 1;}}}return (byte) crc;}}
public static byte CRC8(byte[] buffer,byte poly)
{
byte crc = 0;
byte CRCPoly = poly;
for (int j = 0; j buffer.Length; j++)
{
crc ^= buffer[j];
for (int i = 0; i 8; i++)
{
if ((crc 0x80) != 0)
{
crc = 1;
crc ^= CRCPoly;
}
else
{
crc = 1;
}
}
}
crc = Convert.ToByte(crc ^ 0x00);
return crc;
}
由于多項(xiàng)式的最高為都為1,并且在代碼的crc8計(jì)算中,最高位也是不使用的,
所以在多項(xiàng)式記錄時(shí)都去掉了最高位。
CRC校驗(yàn)算法,說白了,就是把需要校驗(yàn)的數(shù)據(jù)與多項(xiàng)式進(jìn)行循環(huán)異或(XOR),
但進(jìn)行XOR的方式與實(shí)際中數(shù)據(jù)傳輸時(shí),是高位先傳、還是低位先傳有關(guān)。對于數(shù)據(jù)
高位先傳的方式,XOR從數(shù)據(jù)的高位開始,我們就叫它順序異或吧;對于數(shù)據(jù)低位先
傳的方式,XOR從數(shù)據(jù)的低位開始,我們就叫它反序異或吧。兩種不同的異或方式,
即使對應(yīng)相同的多項(xiàng)式,計(jì)算出來的結(jié)果也是不一樣的。
下面以順序異或的例子說明一些計(jì)算的過程:
使用多項(xiàng)式:x8+x5+x4+1(二進(jìn)制為:100110001)
計(jì)算一個(gè)字節(jié):0x11(二進(jìn)制為:00010001)
計(jì)算步驟:
A、 因?yàn)椴捎庙樞虍惢颍孕枰?jì)算的數(shù)據(jù)左移8位,
移位后數(shù)據(jù)為:0001 0001 0000 000
B、 先進(jìn)行高9bit異或(多項(xiàng)式為9bit),0001 0001 0000 0000,因?yàn)楦?bit的
最高bit為0,不需要進(jìn)行異或,同理,接下來的兩bit也是0,也不需要進(jìn)行進(jìn)行異或。
這樣處理后數(shù)據(jù)為:1 0001 0000 0000;
C、 接下來最高位為1,需要進(jìn)行異或操作了
(搬運(yùn)by:簡單的過客)
當(dāng)前文章:go語言crc8代碼 goc編程語言
URL分享:http://chinadenli.net/article20/hjodjo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信小程序、云服務(wù)器、面包屑導(dǎo)航、網(wǎng)站營銷、App開發(fā)、定制開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)