Code Snippets Database
Control Box
Requested snippets
This page displays 20 requested snippets.
You can choose a category, perform a search or generate a Pascal unit containing the snippets by using the Control Box.
- AddHexPrefix
-
Adds a hex prefix to a string of hex digits.
function AddHexPrefix(const HexStr: string): string; begin Result := SysUtils.HexDisplayPrefix + StripHexPrefix(HexStr); end;
Kind of Snippet: RoutineRequired units: SysUtils.Required snippets: StripHexPrefix.See also: StripHexPrefix.Supported Compilers:D2 D3 D4 D5 D6 D7 D2005 D2006 D2007 D2009 D2010 DXE DXE2 Free
Pascal












Prefix used is '$' when compiled on Delphi or '0x' when compiled on C++ Builder. - BufToHex
-
Returns a hexadecimal representation of the bytes in a buffer of a 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; // Result := ''; 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;Kind of Snippet: RoutineRequired units: None.Required snippets: None.Supported Compilers:D2 D3 D4 D5 D6 D7 D2005 D2006 D2007 D2009 D2010 DXE DXE2 Free
Pascal













- BytesToHex
-
Returns a hexadecimal representation of an array of bytes.
function BytesToHex(const Bytes: array of Byte): string; begin Result := BufToHex(Bytes, Length(Bytes)); end;
Kind of Snippet: RoutineRequired units: None.Required snippets: BufToHex.Supported Compilers:D2 D3 D4 D5 D6 D7 D2005 D2006 D2007 D2009 D2010 DXE DXE2 Free
Pascal













- ByteToHex
-
Returns the hexadecimal representation of a byte.
function ByteToHex(const B: Byte): string; begin Result := SysUtils.IntToHex(B, 2 * SizeOf(B)); end;
Kind of Snippet: RoutineRequired units: SysUtils.Required snippets: None.Supported Compilers:D2 D3 D4 D5 D6 D7 D2005 D2006 D2007 D2009 D2010 DXE DXE2 Free
Pascal













- HexByteSize
-
Returns the number of bytes represented by a hexadecimal string.
function HexByteSize(HexStr: string): Cardinal; begin HexStr := StripHexPrefix(HexStr); Result := (Length(HexStr) div 2) + (Length(HexStr) mod 2); end;
Kind of Snippet: RoutineRequired units: None.Required snippets: StripHexPrefix.See also: HexToBuf.Supported Compilers:D2 D3 D4 D5 D6 D7 D2005 D2006 D2007 D2009 D2010 DXE DXE2 Free
Pascal













- HexToBuf
-
Writes the bytes represented by a hexadecimal string into a 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;Kind of Snippet: RoutineRequired units: None.Required snippets: TryHexToBuf.Supported Compilers:D2 D3 D4 D5 D6 D7 D2005 D2006 D2007 D2009 D2010 DXE DXE2 Free
Pascal













- HexToBytes
-
Writes the bytes represented by a hexadecimal string into an array. The array is sized accordingly.
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;Kind of Snippet: RoutineRequired units: None.Supported Compilers:D2 D3 D4 D5 D6 D7 D2005 D2006 D2007 D2009 D2010 DXE DXE2 Free
Pascal












This snippet cannot be included in generated units. - HexToInt
-
Converts a hexadecimal string to an integer. Raises an exception if the string is not valid hexadecimal.
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;Kind of Snippet: RoutineRequired units: None.Required snippets: TryHexToInt.Supported Compilers:D2 D3 D4 D5 D6 D7 D2005 D2006 D2007 D2009 D2010 DXE DXE2 Free
Pascal













- HexToInt64
-
Converts a hexadecimal string to a 64 bit integer. Raises an exception if the string is not valid hexadecimal.
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;Kind of Snippet: RoutineRequired units: None.Required snippets: TryHexToInt64.Supported Compilers:D2 D3 D4 D5 D6 D7 D2005 D2006 D2007 D2009 D2010 DXE DXE2 Free
Pascal













- HexToInt64Def
-
Converts a 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;Kind of Snippet: RoutineRequired units: None.Required snippets: TryHexToInt64.Supported Compilers:D2 D3 D4 D5 D6 D7 D2005 D2006 D2007 D2009 D2010 DXE DXE2 Free
Pascal













- HexToIntDef
-
Converts a 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;Kind of Snippet: RoutineRequired units: None.Required snippets: TryHexToInt.Supported Compilers:D2 D3 D4 D5 D6 D7 D2005 D2006 D2007 D2009 D2010 DXE DXE2 Free
Pascal













- LongWordToHex
-
Returns the hexadecimal representation of a long (4 byte) word.
function LongWordToHex(const LW: LongWord): string; begin Result := SysUtils.IntToHex(Integer(LW), 2 * SizeOf(LW)); end;
Kind of Snippet: RoutineRequired units: SysUtils.Required snippets: None.Supported Compilers:D2 D3 D4 D5 D6 D7 D2005 D2006 D2007 D2009 D2010 DXE DXE2 Free
Pascal













- QuadWordToHex
-
Returns the hexadecimal representation of a quad (8 byte) word.
function QuadWordToHex(const QW: UInt64): string; begin Result := SysUtils.IntToHex(Int64(QW), 2 * SizeOf(QW)); end;
Kind of Snippet: RoutineRequired units: SysUtils.Required snippets: None.Supported Compilers:D2 D3 D4 D5 D6 D7 D2005 D2006 D2007 D2009 D2010 DXE DXE2 Free
Pascal













- StripHexPrefix
-
Strips any prefix from a hex 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;Kind of Snippet: RoutineRequired units: SysUtils.Required snippets: None.See also: AddHexPrefix.Supported Compilers:D2 D3 D4 D5 D6 D7 D2005 D2006 D2007 D2009 D2010 DXE DXE2 Free
Pascal













- TryHexToBuf
-
Writes the bytes represented by a hexadecimal string into a buffer, which must be large enough to receive the data. Returns True on success or False if HexStr is either empty or contains invalid hex characters.
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;Kind of Snippet: RoutineRequired units: None.Supported Compilers:D2 D3 D4 D5 D6 D7 D2005 D2006 D2007 D2009 D2010 DXE DXE2 Free
Pascal













- TryHexToBytes
-
Writes the bytes represented by a hexadecimal string into a byte array. Returns True on success or False if HexStr is either empty or contains invalid hex characters.
function TryHexToBytes(HexStr: string; out Bytes: TBytes): Boolean; begin SetLength(Bytes, HexByteSize(HexStr)); Result := TryHexToBuf(HexStr, Bytes[0]); end;
Kind of Snippet: RoutineRequired units: None.Supported Compilers:D2 D3 D4 D5 D6 D7 D2005 D2006 D2007 D2009 D2010 DXE DXE2 Free
Pascal












This snippet cannot be included in generated units. - TryHexToInt
-
Converts a hexadecimal string to an integer. Returns True if the string was converted successfully and sets 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;
Kind of Snippet: RoutineRequired units: None.Required snippets: AddHexPrefix.Supported Compilers:D2 D3 D4 D5 D6 D7 D2005 D2006 D2007 D2009 D2010 DXE DXE2 Free
Pascal













- TryHexToInt64
-
Converts a hexadecimal string to a 64 bit integer. Returns True if the string was converted successfully and sets 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;
Kind of Snippet: RoutineRequired units: None.Required snippets: AddHexPrefix.Supported Compilers:D2 D3 D4 D5 D6 D7 D2005 D2006 D2007 D2009 D2010 DXE DXE2 Free
Pascal













- WordToHex
-
Returns the hexadecimal representation of a Word (2 bytes).
function WordToHex(const W: Word): string; begin Result := SysUtils.IntToHex(W, 2 * SizeOf(W)); end;
Kind of Snippet: RoutineRequired units: SysUtils.Required snippets: None.Supported Compilers:D2 D3 D4 D5 D6 D7 D2005 D2006 D2007 D2009 D2010 DXE DXE2 Free
Pascal













- 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}Kind of Snippet: Type DefinitionRequired units: SysUtils.Required snippets: None.See also: None.Supported Compilers:D2 D3 D4 D5 D6 D7 D2005 D2006 D2007 D2009 D2010 DXE DXE2 Free
Pascal












This snippet cannot be included in generated units.
