SHARE
TWEET

crc16 delphi windows

roozgar Jul 18th, 2019 83 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function Pow(i, k: Integer): Integer;
  2. var
  3.   j, Count: Integer;
  4. begin
  5.   if k>0 then j:=2
  6.     else j:=1;
  7.   for Count:=1 to k-1 do
  8.     j:=j*2;
  9.   Result:=j;
  10. end;
  11.  
  12. function BinToDec(Str: string): Integer;
  13. var
  14.   Len, Res, i: Integer;
  15.   Error: Boolean;
  16. begin
  17.   Error:=False;
  18.   Len:=Length(Str);
  19.   Res:=0;
  20.   for i:=1 to Len do
  21.     if (Str[i]='0')or(Str[i]='1') then
  22.       Res:=Res+Pow(2, Len-i)*StrToInt(Str[i])
  23.     else
  24.     begin
  25.       //MessageDlg('It is not a binary number', mtInformation, [mbOK], 0);
  26.       Error:=True;
  27.       Break;
  28.     end;
  29.   if Error=True then Result:=0
  30.     else Result:=Res;
  31. end;
  32. //------------------------------------------------------------------------------
  33. function CRC16CCITT(bytes: array of Byte): Word;
  34. const
  35.   polynomial = $1021;
  36. var
  37.   crc: Word;
  38.   I, J: Integer;
  39.   b: Byte;
  40.   bit, c15: Boolean;
  41. begin
  42.   crc := $FFFF;
  43.   for I := 0 to High(bytes) do
  44.   begin
  45.     b := bytes[I];
  46.     for J := 0 to 7 do
  47.     begin
  48.       bit := (((b shr (7-J)) and 1) = 1);
  49.       c15 := (((crc shr 15) and 1) = 1);
  50.       crc := crc shl 1;
  51.       if ((c15 xor bit) <> false) then crc := crc xor polynomial;
  52.     end;
  53.   end;
  54.   Result := crc and $ffff;
  55. end;
  56. //------------------------------------------------------------------------------
  57. function HexToDec(const Str: string): Integer;
  58. begin
  59.   if (Str <> '') and ((Str[1] = '-') or (Str[1] = '+')) then
  60.     Result := StrToInt(Str[1] + '$' + Copy(Str, 2, MaxInt))
  61.   else
  62.     Result := StrToInt('$' + Str);
  63. end;
  64. //------------------------------------------------------------------------------
  65. function bin2crc16(str: string): string;
  66. var
  67. I:integer;
  68. lengthCount : integer;
  69. crcByteArr : array of Byte;
  70. crcOut : Word;
  71. begin
  72.   lengthCount := Trunc(length(str)/8);
  73.   setlength(crcByteArr , lengthCount );
  74.   for I := 0 to lengthCount-1 do
  75.   begin
  76.     crcByteArr[I] := BinToDec(copy(str, I*8+1, 8));
  77.   end;
  78.   crcOut := CRC16CCITT(crcByteArr);
  79.  
  80.   result := crcOut.ToHexString;
  81.  
  82. end;
  83. //------------------------------------------------------------------------------
  84. function HexToBin(Hexadecimal: string): string;
  85. const
  86.   BCD: array [0..15] of string =
  87.     ('0000', '0001', '0010', '0011', '0100', '0101', '0110', '0111',
  88.     '1000', '1001', '1010', '1011', '1100', '1101', '1110', '1111');
  89. var
  90.   i: integer;
  91. begin
  92.   //form2.Memo4.Lines.Add('xin'+Hexadecimal);
  93.   Result := '';
  94.   for i := Length(Hexadecimal) downto 1 do
  95.     Result := BCD[StrToInt('$' + Hexadecimal[i])] + Result;
  96. end;
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top