From Free Pascal wiki
Revision as of 19:06, 12 March 2018 by Kai Burghardt (talk | contribs) (highlight line containing chr)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Deutsch (de) English (en) français (fr) русский (ru)

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

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:

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

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

FPC has implemented chr as a compiler intrinsic in_chr_byte.

see also

  • chr in the system unit reference
  • ord does the reverse operation.