Advertisement
Guest User

Untitled

a guest
Jul 18th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.09 KB | None | 0 0
  1. <?php
  2. function crc16($data)
  3. {
  4. $crc = 0xFFFF;
  5. for ($i = 0; $i < strlen($data); $i++)
  6. {
  7. $x = (($crc >> 8) ^ ord($data[$i])) & 0xFF;
  8. $x ^= $x >> 4;
  9. $crc = (($crc << 8) ^ ($x << 12) ^ ($x << 5) ^ $x) & 0xFFFF;
  10. }
  11. return $crc;
  12. }
  13.  
  14. function CRC16CCITT(bytes: TBytes): Word;
  15. const
  16. polynomial = $1021; // 0001 0000 0010 0001 (0, 5, 12)
  17. var
  18. crc: Word;
  19. I, J: Integer;
  20. b: Byte;
  21. bit, c15: Boolean;
  22. begin
  23. crc := $FFFF; // initial value
  24. for I := 0 to High(bytes) do
  25. begin
  26. b := bytes[I];
  27. for J := 0 to 7 do
  28. begin
  29. bit := (((b shr (7-J)) and 1) = 1);
  30. c15 := (((crc shr 15) and 1) = 1);
  31. crc := crc shl 1;
  32. if ((c15 xor bit) <> 0) then crc := crc xor polynomial;
  33. end;
  34. end;
  35. Result := crc and $ffff;
  36. end;
  37.  
  38. function crc16(Buffer:String;Polynom,Initial:Cardinal):Cardinal;
  39. var
  40. i,j: Integer;
  41. begin
  42. Result:=Initial;
  43. for i:=1 to Length(Buffer) do begin
  44. Result:=Result xor (ord(buffer[i]) shl 8);
  45. for j:=0 to 7 do begin
  46. if (Result and $8000)<>0 then Result:=(Result shl 1) xor Polynom
  47. else Result:=Result shl 1;
  48. end;
  49. end;
  50. Result:=Result and $ffff;
  51. end;
  52.  
  53. unit CRC16CCITT;
  54.  
  55. interface
  56.  
  57. function ComputeCRC16CCITT(crc: word; const data: PByte; len:integer) : word;
  58.  
  59. implementation
  60.  
  61. const
  62. crc16_table: array [0..$FF] of word = (0,4489,8978,12955,17956,22445,25910,29887,35912,40385,44890,48851,51820,56293,59774,
  63. 63735,4225,264,13203,8730,22181,18220,30135,25662,40137,36160,49115,44626,56045,52068,63999,
  64. 59510,8450,12427,528,5017,26406,30383,17460,21949,44362,48323,36440,40913,60270,64231,51324,
  65. 55797,12675,8202,4753,792,30631,26158,21685,17724,48587,44098,40665,36688,64495,60006,55549,
  66. 51572,16900,21389,24854,28831,1056,5545,10034,14011,52812,57285,60766,64727,34920,39393,43898,
  67. 47859,21125,17164,29079,24606,5281,1320,14259,9786,57037,53060,64991,60502,39145,35168,48123,
  68. 43634,25350,29327,16404,20893,9506,13483,1584,6073,61262,65223,52316,56789,43370,47331,35448,
  69. 39921,29575,25102,20629,16668,13731,9258,5809,1848,65487,60998,56541,52564,47595,43106,39673,
  70. 35696,33800,38273,42778,46739,49708,54181,57662,61623,2112,6601,11090,15067,20068,24557,28022,
  71. 31999,38025,34048,47003,42514,53933,49956,61887,57398,6337,2376,15315,10842,24293,20332,32247,
  72. 27774,42250,46211,34328,38801,58158,62119,49212,53685,10562,14539,2640,7129,28518,32495,19572,
  73. 24061,46475,41986,38553,34576,62383,57894,53437,49460,14787,10314,6865,2904,32743,28270,23797,
  74. 19836,50700,55173,58654,62615,32808,37281,41786,45747,19012,23501,26966,30943,3168,7657,12146,
  75. 16123,54925,50948,62879,58390,37033,33056,46011,41522,23237,19276,31191,26718,7393,3432,16371,
  76. 11898,59150,63111,50204,54677,41258,45219,33336,37809,27462,31439,18516,23005,11618,15595,3696,
  77. 8185,63375,58886,54429,50452,45483,40994,37561,33584,31687,27214,22741,18780,15843,11370,7921,
  78. 3960);
  79.  
  80. function ComputeCRC16CCITT(crc: word; const data: PByte; len:integer) : word;
  81. var
  82. i : integer;
  83. begin
  84. for i := 0 to len-1 do
  85. crc := (crc shr 8) xor crc16_table[(crc xor data[i]) and $ff];
  86. result := crc;
  87. end;
  88.  
  89. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement