| 1 | unit UCommon;
|
|---|
| 2 |
|
|---|
| 3 | {$mode Delphi}{$H+}
|
|---|
| 4 |
|
|---|
| 5 | interface
|
|---|
| 6 |
|
|---|
| 7 | uses
|
|---|
| 8 | {$IFDEF Windows}
|
|---|
| 9 | Windows,
|
|---|
| 10 | ShFolder,
|
|---|
| 11 | {$ENDIF} SysUtils;
|
|---|
| 12 |
|
|---|
| 13 | type
|
|---|
| 14 | TArrayOfByte = array of Byte;
|
|---|
| 15 | TArrayOfString = array of string;
|
|---|
| 16 |
|
|---|
| 17 | function IntToBin(Data: Cardinal; Count: Byte): string;
|
|---|
| 18 | function TryHexToInt(Data: string; var Value: Integer): Boolean;
|
|---|
| 19 | function TryBinToInt(Data: string; var Value: Integer): Boolean;
|
|---|
| 20 | {$IFDEF Windows}
|
|---|
| 21 | function GetSpecialFolderPath(Folder: Integer): string;
|
|---|
| 22 | {$ENDIF}
|
|---|
| 23 | function BCDToInt(Value: Byte): Byte;
|
|---|
| 24 | function CompareByteArray(Data1, Data2: TArrayOfByte): Boolean;
|
|---|
| 25 | function Explode(Separator: Char; Data: string; SlicesCount: Integer = -1): TArrayOfString;
|
|---|
| 26 |
|
|---|
| 27 | implementation
|
|---|
| 28 |
|
|---|
| 29 | function BCDToInt(Value: Byte): Byte;
|
|---|
| 30 | begin
|
|---|
| 31 | Result := (Value shr 4) * 10 + (Value and 15);
|
|---|
| 32 | end;
|
|---|
| 33 |
|
|---|
| 34 | {$IFDEF Windows}
|
|---|
| 35 | function GetSpecialFolderPath(Folder: Integer): string;
|
|---|
| 36 | const
|
|---|
| 37 | SHGFP_TYPE_CURRENT = 0;
|
|---|
| 38 | var
|
|---|
| 39 | Path: array[0..MAX_PATH] of Char;
|
|---|
| 40 | begin
|
|---|
| 41 | if SUCCEEDED(SHGetFolderPath(0, Folder, 0, SHGFP_TYPE_CURRENT, @path[0])) then
|
|---|
| 42 | Result := path
|
|---|
| 43 | else
|
|---|
| 44 | Result := '';
|
|---|
| 45 | end;
|
|---|
| 46 | {$ENDIF}
|
|---|
| 47 |
|
|---|
| 48 | function IntToBin(Data: Cardinal; Count: Byte): string;
|
|---|
| 49 | var
|
|---|
| 50 | I: Integer;
|
|---|
| 51 | begin
|
|---|
| 52 | Result := '';
|
|---|
| 53 | for I := 0 to Count - 1 do
|
|---|
| 54 | Result := IntToStr((Data shr I) and 1) + Result;
|
|---|
| 55 | end;
|
|---|
| 56 |
|
|---|
| 57 | function IntToHex(Data: Cardinal; Count: Byte): string;
|
|---|
| 58 | const
|
|---|
| 59 | Chars: array[0..15] of Char = '0123456789ABCDEF';
|
|---|
| 60 | var
|
|---|
| 61 | I: Integer;
|
|---|
| 62 | begin
|
|---|
| 63 | Result := '';
|
|---|
| 64 | for I := 0 to Count - 1 do
|
|---|
| 65 | Result := Result + Chars[(Data shr (I * 4)) and 15];
|
|---|
| 66 | end;
|
|---|
| 67 |
|
|---|
| 68 | function TryHexToInt(Data: string; var Value: Integer): Boolean;
|
|---|
| 69 | var
|
|---|
| 70 | I: Integer;
|
|---|
| 71 | begin
|
|---|
| 72 | Data := UpperCase(Data);
|
|---|
| 73 | Result := True;
|
|---|
| 74 | Value := 0;
|
|---|
| 75 | for I := 0 to Length(Data) - 1 do begin
|
|---|
| 76 | if (Data[I + 1] >= '0') and (Data[I + 1] <= '9') then
|
|---|
| 77 | Value := Value or (Ord(Data[I + 1]) - Ord('0')) shl ((Length(Data) - I - 1) * 4)
|
|---|
| 78 | else if (Data[I + 1] >= 'A') and (Data[I + 1] <= 'F') then
|
|---|
| 79 | Value := Value or (Ord(Data[I + 1]) - Ord('A') + 10) shl ((Length(Data) - I - 1) * 4)
|
|---|
| 80 | else Result := False;
|
|---|
| 81 | end;
|
|---|
| 82 | end;
|
|---|
| 83 |
|
|---|
| 84 | function TryBinToInt(Data: string; var Value: Integer): Boolean;
|
|---|
| 85 | var
|
|---|
| 86 | I: Integer;
|
|---|
| 87 | begin
|
|---|
| 88 | Result := True;
|
|---|
| 89 | Value := 0;
|
|---|
| 90 | for I := 0 to Length(Data) - 1 do begin
|
|---|
| 91 | if (Data[I + 1] >= '0') and (Data[I + 1] <= '1') then
|
|---|
| 92 | Value := Value or (Ord(Data[I + 1]) - Ord('0')) shl ((Length(Data) - I - 1))
|
|---|
| 93 | else Result := False;
|
|---|
| 94 | end;
|
|---|
| 95 | end;
|
|---|
| 96 |
|
|---|
| 97 | function CompareByteArray(Data1, Data2: TArrayOfByte): Boolean;
|
|---|
| 98 | var
|
|---|
| 99 | I: Integer;
|
|---|
| 100 | begin
|
|---|
| 101 | if Length(Data1) = Length(Data2) then begin
|
|---|
| 102 | Result := True;
|
|---|
| 103 | for I := 0 to Length(Data1) - 1 do begin
|
|---|
| 104 | if Data1[I] <> Data2[I] then begin
|
|---|
| 105 | Result := False;
|
|---|
| 106 | Break;
|
|---|
| 107 | end
|
|---|
| 108 | end;
|
|---|
| 109 | end else Result := False;
|
|---|
| 110 | end;
|
|---|
| 111 |
|
|---|
| 112 | function Explode(Separator: char; Data: string; SlicesCount: Integer = -1): TArrayOfString;
|
|---|
| 113 | begin
|
|---|
| 114 | SetLength(Result, 0);
|
|---|
| 115 | while (Pos(Separator, Data) > 0) and
|
|---|
| 116 | ((Length(Result) < (SlicesCount - 1)) or (SlicesCount = -1)) do begin
|
|---|
| 117 | SetLength(Result, Length(Result) + 1);
|
|---|
| 118 | Result[High(Result)] := Copy(Data, 1, Pos(Separator, Data) - 1);
|
|---|
| 119 | Delete(Data, 1, Pos(Separator, Data));
|
|---|
| 120 | end;
|
|---|
| 121 | SetLength(Result, Length(Result) + 1);
|
|---|
| 122 | Result[High(Result)] := Data;
|
|---|
| 123 | end;
|
|---|
| 124 |
|
|---|
| 125 | end.
|
|---|