From Lazarus wiki
Jump to navigationJump to search

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.