Chr

From Free Pascal wiki
Revision as of 19:06, 12 March 2018 by Kai Burghardt (talk | contribs) (highlight line containing chr)
Jump to navigationJump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

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:

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

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