Andreas Software - Source Code - DataTools.PAS

A simple little library to handle some common data types in Embarcadero Delphi

unit DataTools;

{
  A simple little library to handle some common data types in Embarcadero Delphi.

  Note: You may need to reverse the order when using the library to suit your needs regarding Endianness.

  Original Author: Andreas Jonsson. (www.andreas-software.com)

  NOTICE: YOU MAY USE AND MODIFY THIS SOURCE CODE FREELY, THE ONLY REQUIREMENT IS THAT THE COMMENT ABOUT THE ORIGINAL AUTHOR AND THIS NOTICE REMAINS IN THE SOURCE CODE.
}

interface

uses
  System.SysUtils, System.Variants, System.Classes;

const ENCODING_HEX     = '0123456789ABCDEF';
const ENCODING_LETTERS = 'GHJKLMNPQRSTVWXZ';
const ENCODING_SYMBOLS = '!@#$/=?+\*|<>-_:';

procedure SwapBytes(var A: Byte; var B: Byte); inline;
procedure SwapWords(var A: Word; var B: Word); inline;
procedure SwapCardinals(var A: Cardinal; var B: Cardinal); inline;
procedure SwapUInt64s(var A: UInt64; var B: UInt64); inline;

function ByteNibble0(B: Byte): Byte; inline;
function ByteNibble1(B: Byte): Byte; inline;

function MakeByte(Nibble0: Byte; Nibble1: Byte): Byte; inline;

function WordByte0(W: Word): Byte; inline;
function WordByte1(W: Word): Byte; inline;

function MakeWord(B0: Byte; B1: Byte): Word; inline;

function CardinalByte0(C: Cardinal): Byte; inline;
function CardinalByte1(C: Cardinal): Byte; inline;
function CardinalByte2(C: Cardinal): Byte; inline;
function CardinalByte3(C: Cardinal): Byte; inline;

function MakeCardinal(B0: Byte; B1: Byte; B2: Byte; B3: Byte): Cardinal; inline;

function UInt64Byte0(U: UInt64): Byte; inline;
function UInt64Byte1(U: UInt64): Byte; inline;
function UInt64Byte2(U: UInt64): Byte; inline;
function UInt64Byte3(U: UInt64): Byte; inline;
function UInt64Byte4(U: UInt64): Byte; inline;
function UInt64Byte5(U: UInt64): Byte; inline;
function UInt64Byte6(U: UInt64): Byte; inline;
function UInt64Byte7(U: UInt64): Byte; inline;

function MakeUInt64(B0: Byte; B1: Byte; B2: Byte; B3: Byte; B4: Byte; B5: Byte; B6: Byte; B7: Byte): UInt64; inline;

function HiNibble(B: Byte): Byte; inline;
function LoNibble(B: Byte): Byte; inline;

function TwoNibblesToByte(Hi: Byte; Lo: Byte): Byte; inline;

function HiByte(W: Word): Byte; inline;
function LoByte(W: Word): Byte; inline;

function TwoBytesToWord(Hi: Byte; Lo: Byte): Word; inline;

function HiWord(C: Cardinal): Word; inline;
function LoWord(C: Cardinal): Word; inline;

function TwoWordsToCardinal(Hi: Word; Lo: Word): Cardinal; inline;

function HiCardinal(U: UInt64): Cardinal; inline;
function LoCardinal(U: UInt64): Cardinal; inline;

function TwoCardinalsToUInt64(Hi: Cardinal; Lo: Cardinal): UInt64; inline;

function RotateLeftByte(Value: Byte; N: Byte): Byte; inline;
function RotateRightByte(Value: Byte; N: Byte): Byte; inline;

function RotateLeftWord(Value: Word; N: Word): Word; inline;
function RotateRightWord(Value: Word; N: Word): Word; inline;

function RotateLeftCardinal(Value: Cardinal; N: Cardinal): Cardinal; inline;
function RotateRightCardinal(Value: Cardinal; N: Cardinal): Cardinal; inline;

function RotateLeftUInt64(Value: UInt64; N: UInt64): UInt64; inline;
function RotateRightUInt64(Value: UInt64; N: UInt64): UInt64; inline;

function ByteToHex(B: Byte; SixteenCharacters: string = ENCODING_HEX): string;
function HexToByte(S: string; SixteenCharacters: string = ENCODING_HEX; CaseSensitive: Boolean = True): Byte;

implementation

procedure SwapBytes(var A: Byte; var B: Byte); inline;
begin
  A := A xor B;
  B := A xor B;
  A := A xor B;
end;

procedure SwapWords(var A: Word; var B: Word); inline;
begin
  A := A xor B;
  B := A xor B;
  A := A xor B;
end;

procedure SwapCardinals(var A: Cardinal; var B: Cardinal); inline;
begin
  A := A xor B;
  B := A xor B;
  A := A xor B;
end;

procedure SwapUInt64s(var A: UInt64; var B: UInt64); inline;
begin
  A := A xor B;
  B := A xor B;
  A := A xor B;
end;

function ByteNibble0(B: Byte): Byte; inline;
begin
  Result := (B shr 4);
end;

function ByteNibble1(B: Byte): Byte; inline;
begin
  Result := (B and $F);
end;

function MakeByte(Nibble0: Byte; Nibble1: Byte): Byte; inline;
begin
  Result := ((Nibble0 shl 4) or Nibble1);
end;

function WordByte0(W: Word): Byte; inline;
begin
  Result := (W shr 8);
end;

function WordByte1(W: Word): Byte; inline;
begin
  Result := (W and $FF);
end;

function MakeWord(B0: Byte; B1: Byte): Word; inline;
begin
  Result := ((Word(B0) shl 8) or B1);
end;

function CardinalByte0(C: Cardinal): Byte; inline;
begin
  Result := (C shr 24);
end;

function CardinalByte1(C: Cardinal): Byte; inline;
begin
  Result := ((C shr 16) and $FF);
end;

function CardinalByte2(C: Cardinal): Byte; inline;
begin
  Result := ((C shr 8) and $FF);
end;

function CardinalByte3(C: Cardinal): Byte; inline;
begin
  Result := (C and $FF);
end;

function MakeCardinal(B0: Byte; B1: Byte; B2: Byte; B3: Byte): Cardinal; inline;
begin
  Result := ((Cardinal(B0) shl 24) or (Cardinal(B1) shl 16) or (Cardinal(B2) shl 8) or B3);
end;

function UInt64Byte0(U: UInt64): Byte; inline;
begin
  Result := (U shr 56);
end;

function UInt64Byte1(U: UInt64): Byte; inline;
begin
  Result := ((U shr 48) and $FF);
end;

function UInt64Byte2(U: UInt64): Byte; inline;
begin
  Result := ((U shr 40) and $FF);
end;

function UInt64Byte3(U: UInt64): Byte; inline;
begin
  Result := ((U shr 32) and $FF);
end;

function UInt64Byte4(U: UInt64): Byte; inline;
begin
  Result := ((U shr 24) and $FF);
end;

function UInt64Byte5(U: UInt64): Byte; inline;
begin
  Result := ((U shr 16) and $FF);
end;

function UInt64Byte6(U: UInt64): Byte; inline;
begin
  Result := ((U shr 8) and $FF);
end;

function UInt64Byte7(U: UInt64): Byte; inline;
begin
  Result := (U and $FF);
end;

function MakeUInt64(B0: Byte; B1: Byte; B2: Byte; B3: Byte; B4: Byte; B5: Byte; B6: Byte; B7: Byte): UInt64; inline;
begin
  Result := ((UInt64(B0) shl 56) or (UInt64(B1) shl 48) or (UInt64(B2) shl 40) or (UInt64(B3) shl 32) or (UInt64(B4) shl 24) or (UInt64(B5) shl 16) or (UInt64(B6) shl 8) or B7);
end;

function HiNibble(B: Byte): Byte; inline;
begin
  Result := (B shr 4);
end;

function LoNibble(B: Byte): Byte; inline;
begin
  Result := (B and $F);
end;

function TwoNibblesToByte(Hi: Byte; Lo: Byte): Byte; inline;
begin
  Result := ((Hi shl 4) or Lo);
end;

function HiByte(W: Word): Byte; inline;
begin
  Result := (W shr 8);
end;

function LoByte(W: Word): Byte; inline;
begin
  Result := (W and $FF);
end;

function TwoBytesToWord(Hi: Byte; Lo: Byte): Word; inline;
begin
  Result := ((Word(Hi) shl 8) or Lo);
end;

function HiWord(C: Cardinal): Word; inline;
begin
  Result := (C shr 16);
end;

function LoWord(C: Cardinal): Word; inline;
begin
  Result := (C and $FFFF);
end;

function TwoWordsToCardinal(Hi: Word; Lo: Word): Cardinal; inline;
begin
  Result := ((Cardinal(Hi) shl 16) or Lo);
end;

function HiCardinal(U: UInt64): Cardinal; inline;
begin
  Result := (U shr 32);
end;

function LoCardinal(U: UInt64): Cardinal; inline;
begin
  Result := (U and $FFFFFFFF);
end;

function TwoCardinalsToUInt64(Hi: Cardinal; Lo: Cardinal): UInt64; inline;
begin
  Result := ((UInt64(Hi) shl 32) or Lo);
end;

function RotateLeftByte(Value: Byte; N: Byte): Byte; inline;
begin
  Result := ((Value shl N) or (Value shr (8 - N)));
end;

function RotateRightByte(Value: Byte; N: Byte): Byte; inline;
begin
  Result := ((Value shr N) or (Value shl (8 - N)));
end;

function RotateLeftWord(Value: Word; N: Word): Word; inline;
begin
  Result := ((Value shl N) or (Value shr (16 - N)));
end;

function RotateRightWord(Value: Word; N: Word): Word; inline;
begin
  Result := ((Value shr N) or (Value shl (16 - N)));
end;

function RotateLeftCardinal(Value: Cardinal; N: Cardinal): Cardinal; inline;
begin
  Result := ((Value shl N) or (Value shr (32 - N)));
end;

function RotateRightCardinal(Value: Cardinal; N: Cardinal): Cardinal; inline;
begin
  Result := ((Value shr N) or (Value shl (32 - N)));
end;

function RotateLeftUInt64(Value: UInt64; N: UInt64): UInt64; inline;
begin
  Result := ((Value shl N) or (Value shr (64 - N)));
end;

function RotateRightUInt64(Value: UInt64; N: UInt64): UInt64; inline;
begin
  Result := ((Value shr N) or (Value shl (64 - N)));
end;

function ByteToHex(B: Byte; SixteenCharacters: string = ENCODING_HEX): string;
begin
  Result := SixteenCharacters[1 + ByteNibble0(B)] + SixteenCharacters[1 + ByteNibble1(B)];
end;

function HexToByte(S: string; SixteenCharacters: string = ENCODING_HEX; CaseSensitive: Boolean = True): Byte;
var Nibble0: Byte;
var Nibble1: Byte;
var I: Byte;
begin
  Result := 0;
  Nibble0 := 0;
  Nibble1 := 0;
  if CaseSensitive <> True then begin
    S := S.ToUpper;
    SixteenCharacters := SixteenCharacters.ToUpper;
  end;
  for I := 1 to 16 do begin
    if S[1] = SixteenCharacters[I] then begin
      Nibble0 := I;
      Break;
    end;
  end;
  for I := 1 to 16 do begin
    if S[2] = SixteenCharacters[I] then begin
      Nibble1 := I;
      Break;
    end;
  end;
  if (Nibble0 <> 0) and (Nibble1 <> 0) then begin
    Nibble0 := Nibble0 - 1;
    Nibble1 := Nibble1 - 1;
    Result := MakeByte(Nibble0, Nibble1);
  end;
end;

end.

⎙ Print DataTools.PAS

You can also view the source code as plain text, DataTools.PAS.TXT.

The source code is related to N/A.

www.andreas-software.com

Copyright © 1998-2021, Andreas Jönsson. All rights reserved.

In Swedish?
View this page in Swedish