Name

Ord Function

Syntax

function Ord(A: AnsiChar): Integer;
function Ord(C: Char): Integer;
function Ord(W: WideChar): Integer;
function Ord(E: Enumerated type): Integer;
function Ord(I: Integer): Integer;
function Ord(I: Int64): Int64;

Description

The Ord function returns the ordinal value of a character or enumeration as a non-negative integer. Calling Ord on an integer argument is a no-op, returning its argument. Ord is not a real function.

Tips and Tricks

You can cast an ordinal variable to an integer type to get its ordinal value. Calling the Ord function is better because it states clearly and directly what the code is doing.

Example

// The TypInfo unit provides the GetEnumName function that returns
// the name of an enumerated value, given its TypeInfo pointer and
// its ordinal value. Using GetEnumName, you can write functions
// such as the following, which converts a Boolean to a string.
function BoolToStr(B: Boolean): string;
begin
  Result := GetEnumName(TypeInfo(Boolean), Ord(B));
end;

See Also

AnsiChar Type, Char Type, Chr Function, Type Keyword, WideChar Type

Get Delphi in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.