Sign in Join
Wiki copytemporary

Chr

Deutsch · English · Français · Русский

The function chr returns the char which has ASCII value b. The signature reads:

pascal
function chr(b: byte): char;

The function is used and necessary because of Pascal's strong type safety. With the advent of typecasts a general method became available, though using chr is better style.

A trivial example shall demonstrate the usage of chr:

pascal
type
    // modern Latin alphabet has 26 letters
    latinAlphabetCharacterIndex = 0..25;

{$push}
// let compiler generate code ensuring n >= 0 and n < 26
{$rangeChecks on}
function getChrInLatinAlphabet(const n: latinAlphabetCharacterIndex): char;
begin
    getChrInLatinAlphabet := chr(ord('A') + n);
end;
{$pop}

FPC has implemented chr as a compiler intrinsic in_chr_byte.

see also


From the Free Pascal wiki, page "Chr", as it stood on 2022-08-06 after a change by Kai Burghardt, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's Licensing page. If you have concerns about this material, message the admin.

Revision 1, last changed by WikiArchive .

Talk about this page

Nobody has opened a bench for this page yet. Sign in to open one.