Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function BinToDec(Str: string): Integer;
- var
- Len, Res, i: Integer;
- Error: Boolean;
- begin
- Error:=False;
- Len:=Length(Str);
- Res:=0;
- for i:=0 to Len-1 do
- if (Str[i]='0')or(Str[i]='1') then
- Res:=Res+Pow(2, Len-i)*StrToInt(Str[i])
- else
- begin
- Error:=True;
- Break;
- end;
- if Error=True then Result:=0
- else Result:=Res;
- end;
- //------------------------------------------------------------------------------
- function CRC16CCITT(bytes: array of Byte): Word;
- const
- polynomial = $1021;
- var
- crc: Word;
- I, J: Integer;
- b: Byte;
- bit, c15: Boolean;
- begin
- crc := $FFFF;
- for I := 0 to High(bytes) do
- begin
- b := bytes[I];
- for J := 0 to 7 do
- begin
- bit := (((b shr (7-J)) and 1) = 1);
- c15 := (((crc shr 15) and 1) = 1);
- crc := crc shl 1;
- if ((c15 xor bit) <> false) then crc := crc xor polynomial;
- end;
- end;
- Result := crc and $ffff;
- end;
- //------------------------------------------------------------------------------
- function bin2crc16(str: string): string;
- var
- I:integer;
- lengthCount : integer;
- crcByteArr : array of Byte;
- crcOut : Word;
- begin
- lengthCount := Trunc(length(str)/8);
- setlength(crcByteArr , lengthCount );
- for I := 0 to lengthCount-1 do
- begin
- crcByteArr[I] := BinToDec(copy(str, I*8, 8));
- end;
- crcOut := CRC16CCITT(crcByteArr);
- result := crcOut.ToHexString;
- end;
- //-----------------------------------------------------------------------------------
- function HexToBin(Hexadecimal: string): string;
- const
- BCD: array [0..15] of string =
- ('0000', '0001', '0010', '0011', '0100', '0101', '0110', '0111',
- '1000', '1001', '1010', '1011', '1100', '1101', '1110', '1111');
- var
- i: integer;
- begin
- //form2.Memo4.Lines.Add('xin'+Hexadecimal);
- Result := '';
- for i := Length(Hexadecimal) downto 1 do
- Result := BCD[StrToInt('$' + Hexadecimal[i])] + Result;
- end;
- //---------------------------------------------------------------------------------
- function Pow(i, k: Integer): Integer;
- var
- j, Count: Integer;
- begin
- if k>0 then j:=2
- else j:=1;
- for Count:=1 to k-1 do
- j:=j*2;
- Result:=j;
- end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement