Home > Back-end >  Delphi CRC16 look-up table method to calculate the problem
Delphi CRC16 look-up table method to calculate the problem

Time:09-25

Look at the Java code implementation methods:

/* * */CRC value
Private int value=https://bbs.csdn.net/topics/0xffff;

Private static int CRC16_TABLE []={
X2312 x0000 0, 0 x1189, 0, 0 x329b, 0 x4624, 0 x57ad, 0 x6536, 0 x74bf,
Xaf5a x8c48 0, 0 x9dc1, 0, 0 xbed3, 0 xca6c, 0 xdbe5, 0 xe97e, 0 xf8f7,
X3393 x1081 0, 0 x0108, 0, 0 x221a, 0 x56a5, 0 x472c, 0 x75b7, 0 x643e,
XBFDB x9cc9 0, 0 x8d40, 0, 0 xae52, 0 xdaed, 0 xcb64, 0 xf9ff, 0 xe876,
X0210 x2102 0, 0 x308b, 0, 0 x1399, 0 x6726, 0 x76af, 0 x4434, 0 x55bd,
X8e58 xad4a 0, 0 xbcc3, 0, 0 x9fd1, 0 xeb6e, 0 xfae7, 0 xc87c, 0 xd9f5,
X1291 x3183 0, 0 x200a, 0, 0 x0318, 0 x77a7, 0 x662e, 0 x54b5, 0 x453c,
X9ed9 XBDCB 0, 0 xac42, 0, 0 x8f50, 0 xfbef, 0 xea66, 0 xd8fd, 0 xc974,
X6116 x4204 0, 0 x538d, 0, 0 x709f, 0 x0420, 0 x15a9, 0 x2732, 0 x36bb,
Xed5e xce4c 0, 0 xdfc5, 0, 0 xfcd7, 0 x8868, 0 x99e1, 0 xab7a, 0 xbaf3,
X7197 x5285 0, 0 x430c, 0, 0 x601e, 0 x14a1, 0 x0528, 0 x37b3, 0 x263a,
XFDDF xdecd 0, 0 xcf44, 0, 0 xec56, 0 x98e9, 0 x8960, 0 XBBFB, 0 xaa72,
X4014 x6306 0, 0 x728f, 0, 0 x519d, 0 x2522, 0 x34ab, 0 x0630, 0 x17b9,
Xcc5c xef4e 0, 0 xfec7, 0, 0 xddd5, 0 xa96a, 0 xb8e3, 0 x8a78, 0 x9bf1,
X5095 x7387 0, 0 x620e, 0, 0 x411c, 0 x35a3, 0 x242a, 0 x16b1, 0 x0738,
XDCDD XFFCF 0, 0 xee46, 0, 0 xcd54, 0 xb9eb, 0 xa862, 0 x9af9, 0 x8b70,
Xa71a x8408 0, 0 x9581, 0, 0 xb693, 0 xc22c, 0 xd3a5, 0 xe13e, 0 xf0b7,
X2b52 x0840 0, 0 x19c9, 0, 0 x3adb, 0 x4e64, 0 x5fed, 0 x6d76, 0 x7cff,
Xb79b x9489 0, 0 x8500, 0, 0 xa612, 0 xd2ad, 0 xc324, 0 xf1bf, 0 xe036,
X3bd3 x18c1 0, 0 x0948, 0, 0 x2a5a, 0 x5ee5, 0 x4f6c, 0 x7df7, 0 x6c7e,
X8618 xa50a 0, 0 xb483, 0, 0 x9791, 0 xe32e, 0 xf2a7, 0 xc03c, 0 xd1b5,
X0a50 x2942 0, 0 x38cb, 0, 0 x1bd9, 0 x6f66, 0 x7eef, 0 x4c74, 0 x5dfd,
X9699 xb58b 0, 0 xa402, 0, 0 x8710, 0 xf3af, 0 xe226, 0 xd0bd, 0 xc134,
X1ad1 x39c3 0, 0 x284a, 0, 0 x0b58, 0 x7fe7, 0 x6e6e, 0 x5cf5, 0 x4d7c,
Xe51e xc60c 0, 0 xd785, 0, 0 xf497, 0 x8028, 0 x91a1, 0 xa33a, 0 xb2b3,
X6956 x4a44 0, 0 x5bcd, 0, 0 x78df, 0 x0c60, 0 x1de9, 0 x2f72, 0 x3efb,
Xf59f xd68d 0, 0 xc704, 0, 0 xe416, 0 x90a9, 0 x8120, 0 xb3bb, 0 xa232,
X79d7 x5ac5 0, 0 x4b4c, 0, 0 x685e, 0 x1ce1, 0 x0d68, 0 x3ff3, 0 x2e7a,
Xc41c xe70e 0, 0 xf687, 0, 0 xd595, 0 xa12a, 0 xb0a3, 0 x8238, 0 x93b1,
X4854 x6b46 0, 0 x7acf, 0, 0 x59dd, 0 x2d62, 0 x3ceb, 0 x0e70, 0 x1ff9,
Xd49d xf78f 0, 0 xe606, 0, 0 xc514, 0 xb1ab, 0 xa022, 0 x92b9, 0 x8330,
X58d5 x7bc7 0, 0 x6a4e, 0, 0 x495c, 0 x3de3, 0 x2c6a, 0 x1ef1, 0 x0f78
};

/* *
* a byte array of CRC calculation note: this byte array is the hexadecimal string into a byte array
* @ param data
*/
Public void update (byte [] data) {
//int FCS=0 XFFFF;
for (int i=0; i Value moves to the right//1. 8 (divided by 256)
//2. The value with the incoming data is exclusive or operation and operation with 0 XFF again after
//get an index index, and then find CRC16_TABLE table corresponding index data
//data from 1 and 2 for exclusive or operation,
Value=https://bbs.csdn.net/topics/(value> 8) ^ CRC16_TABLE [(value ^ data [I]) & 0 XFF];
}
//get the
//return - FCS;
}

The above code to implementation under the Delphi 7, hope the teacher for help;

For example:

Hex: 0 d0108651200000054700002 can get CRC: E1CC
0 d0108651200000054700003 can get CRC: F045
Or

Hex: 0 d01303836353132303030303030353437300002 can get CRC: E1CC

0 d01303836353132303030303030353437300003 can get CRC: F045

My code is as follows: (CRC value in)
The function Build_CRC (const szData: String) : String;
Var
Bdata: an array of byte;
Lv_i lv_j, FCS: Integer;
The begin
Lv_i:=Trunc (Length (szData)/2);
SetLength (Bdata, lv_i);
FCS:=$FFFF;
For lv_j:=0 to do lv_i - 1
The begin
Bdata [lv_j] :=StrToInt (' $' + szData [lv_j * 2 + 1) + szData [lv_j * 2 + 2]).
//showmessage (char (Bdata [lv_j]));
FCS:=(FCS SHR 8) xor CRCTable16 [(FCS xor Bdata [lv_j]) and $FF];
end;
Showmessage (IntToHex (FCS, 4));
end;

CodePudding user response:

A look-up table method that an implementation:

The unit unCRC16;

Interface

The function CalCRC16 (AData: an array of Byte; AStart, AEnd: Integer) : Word;

Implementation

The function CalCRC16 (AData: an array of Byte; AStart, AEnd: Integer) : Word;
Const
GENP=$A001;

Var
CRC: Word;
I: Integer;
TMP: Byte;

Procedure CalOneByte (AByte: Byte);
Var
J: Integer;
The begin
CRC:=CRC xor AByte;
For j:=0 to 7 do
The begin
TMP:=CRC and 1;
The CRC: CRC SHR=1;
CRC:=CRC and $7 FFF;
If TMP=1 then
CRC:=CRC xor GENP;
CRC:=CRC and $FFFF;
end;
end;

The begin
CRC:=$FFFF;
For I:=AStart to AEnd do
CalOneByte (AData [I]);
Result:=CRC;
end;

End.

If the generating polynomial, the GENP=$A001; This value is not the same, such as: $1021

Online can also find a lot of CRC code

CodePudding user response:

The function Build_CRC (const szData: String) : String;
Var
Bdata: an array of byte;
Lv_i lv_j, FCS: Integer;
Pword: Word;
The begin
Lv_i:=Trunc (Length (szData)/2);
SetLength (Bdata, lv_i);
//FCS:=$FFFF;
For lv_j:=0 to do lv_i - 1
The begin
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related