DelphiDabbler Code Snippets Database

Snippet Selection

The following snippets from the Code Snippets Database have been requested.

AddHexPrefix

Adds a valid hex prefix to the given string of hex digits.

function AddHexPrefix(const HexStr: string): string;
begin
  Result := SysUtils.HexDisplayPrefix + StripHexPrefix(HexStr);
end;

BufToHex

Returns a hexadecimal representation of the bytes in the given buffer which has the given size.

function BufToHex(const Buf; const Size: Cardinal): string;
const
  // maps nibbles to hex digits
  cHexDigits: array[$0..$F] of Char = (
    '0', '1', '2', '3', '4', '5', '6', '7',
    '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
  );
var
  I: Cardinal;  // loops thru output string
  PB: ^Byte;    // addresses each byte in buffer
begin
  PB := @Buf;
  SetLength(Result, 2 * Size);
  I := 1;
  while I <= 2 * Size do
  begin
    Result[I] := cHexDigits[PB^ shr 4];
    Result[I + 1] := cHexDigits[PB^ and $0F];
    Inc(PB);
    Inc(I, 2);
  end;
end;

BytesToHex

Returns a hexadecimal representation of the given array of bytes.

function BytesToHex(const Bytes: array of Byte): string;
begin
  Result := BufToHex(Bytes, Length(Bytes));
end;

ByteToHex

Returns the hexadecimal representation of the given byte value.

function ByteToHex(const B: Byte): string;
begin
  Result := SysUtils.IntToHex(B, 2 * SizeOf(B));
end;

HexByteSize

Returns the number of bytes represented by the given hexadecimal string.

function HexByteSize(HexStr: string): Cardinal;
begin
  HexStr := StripHexPrefix(HexStr);
  Result := (Length(HexStr) div 2) + (Length(HexStr) mod 2);
end;

HexToBuf

Writes the bytes represented by the given hexadecimal string into the given buffer, which must be large enough to receive the data.

procedure HexToBuf(HexStr: string; var Buf);
{$IFDEF FPC}
const
{$ELSE}
resourcestring
{$ENDIF}
  sHexConvertError = '''%s'' is not a valid hexadecimal string';
begin
  if not TryHexToBuf(HexStr, Buf) then
    raise SysUtils.EConvertError.CreateFmt(sHexConvertError, [HexStr]);
end;

HexToBytes

Writes the bytes represented by the given hexadecimal string into an array of bytes, which is returned

function HexToBytes(HexStr: string): TBytes;
{$IFDEF FPC}
const
{$ELSE}
resourcestring
{$ENDIF}
  sHexConvertError = '''%s'' is not a valid hexadecimal string';
begin
  if not TryHexToBytes(HexStr, Result) then
    raise SysUtils.EConvertError.CreateFmt(sHexConvertError, [HexStr]);
end;

HexToInt

Converts the given hexadecimal string to an integer.

function HexToInt(const HexStr: string): Integer;
{$IFDEF FPC}
const
{$ELSE}
resourcestring
{$ENDIF}
  sHexConvertError = '''%s'' is not a valid hexadecimal value';
begin
  if not TryHexToInt(HexStr, Result) then
    raise SysUtils.EConvertError.CreateFmt(sHexConvertError, [HexStr]);
end;

HexToInt64

Converts the given hexadecimal string to a 64 bit integer.

function HexToInt64(const HexStr: string): Int64;
{$IFDEF FPC}
const
{$ELSE}
resourcestring
{$ENDIF}
  sHexConvertError = '''%s'' is not a valid hexadecimal value';
begin
  if not TryHexToInt64(HexStr, Result) then
    raise SysUtils.EConvertError.CreateFmt(sHexConvertError, [HexStr]);
end;

HexToInt64Def

Converts the given hexadecimal string to a 64 bit integer. If the conversion fails the specified default value is returned.

function HexToInt64Def(const HexStr: string; const Default: Int64): Int64;
begin
  if not TryHexToInt64(HexStr, Result) then
    Result := Default;
end;

HexToIntDef

Converts the given hexadecimal string to an integer. If the conversion fails the specified default value is returned.

function HexToIntDef(const HexStr: string; const Default: Integer): Integer;
begin
  if not TryHexToInt(HexStr, Result) then
    Result := Default;
end;

LongWordToHex

Returns the hexadecimal representation of the given long (4 byte) word value.

function LongWordToHex(const LW: LongWord): string;
begin
  Result := SysUtils.IntToHex(Integer(LW), 2 * SizeOf(LW));
end;

QuadWordToHex

Returns the hexadecimal representation of the given quad (8 byte) word value.

function QuadWordToHex(const QW: UInt64): string;
begin
  Result := SysUtils.IntToHex(Int64(QW), 2 * SizeOf(QW));
end;

StripHexPrefix

Strips any prefix from the given hexadecimal string. Both Delphi ('$') and C ('0x') prefixes are supported.

function StripHexPrefix(const HexStr: string): string;
begin
  if Pos('$', HexStr) = 1 then
    Result := Copy(HexStr, 2, Length(HexStr) - 1)
  else if Pos('0x', SysUtils.LowerCase(HexStr)) = 1 then
    Result := Copy(HexStr, 3, Length(HexStr) - 2)
  else
    Result := HexStr;
end;

TryHexToBuf

Attempts to convert the given hexadecimal string into a binary value that is written to buffer Buf. Returns True on success or False on error, leaving Buf in an unknown state. Buf must be large enough to receive all the converted data.

function TryHexToBuf(HexStr: string; var Buf): Boolean;
var
  I: Integer;       // loops through characters of string
  PB: ^Byte;        // references each byte in buffer
  ByteVal: Integer; // a byte value from hex string
begin
  Result := False;
  HexStr := StripHexPrefix(HexStr);
  if HexStr = '' then
    Exit;
  if Odd(Length(HexStr)) then
    HexStr := '0' + HexStr;
  I := 1;
  PB := @Buf;
  while I <= Length(HexStr) do
  begin
    if not TryHexToInt(HexStr[I] + HexStr[I + 1], ByteVal) then
      Exit;
    PB^ := Byte(ByteVal);
    Inc(I, 2);
    Inc(PB);
  end;
  Result := True;
end;

TryHexToBytes

Attempts to convert the given hexadecimal string into an array of bytes. Returns True on success and passes back resulting array in Bytes. Returns False on error, leaving Bytes underfined.

function TryHexToBytes(HexStr: string; out Bytes: TBytes): Boolean;
begin
  SetLength(Bytes, HexByteSize(HexStr));
  Result := TryHexToBuf(HexStr, Bytes[0]);
end;

TryHexToInt

Attempts to convert the given hexadecimal string into a 32 bit integer. Returns True on success and set Value to the converted value. Returns False on error, leaving Value undefined.

function TryHexToInt(const HexStr: string; out Value: Integer): Boolean;
var
  E: Integer; // error code
begin
  Val(AddHexPrefix(HexStr), Value, E);
  Result := E = 0;
end;

TryHexToInt64

Attempts to convert the given hexadecimal string into a 64 bit integer. Returns True on success and set Value to the converted value. Returns False on error, leaving Value undefined.

function TryHexToInt64(const HexStr: string; out Value: Int64): Boolean;
var
  E: Integer; // error code
begin
  Val(AddHexPrefix(HexStr), Value, E);
  Result := E = 0;
end;

WordToHex

Returns the hexadecimal representation of the given (2 byte) word value.

function WordToHex(const W: Word): string;
begin
  Result := SysUtils.IntToHex(W, 2 * SizeOf(W));
end;

View the whole database.

Go to the DelphiDabbler website.