DelphiDabbler Code Snippets Database

Snippet Selection

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

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;

TBytes

Dynamic array of bytes. Ensures TBytes is available on non-Unicode compilers that don't define it in SysUtils.

type
  {$IFDEF UNICODE}
  TBytes = SysUtils.TBytes;
  {$ELSE}
  TBytes = array of Byte;
  {$ENDIF}

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;

View the whole database.

Go to the DelphiDabbler website.