Difference between revisions of "Chr"

From Lazarus wiki
Jump to navigationJump to search
(review)
m (highlight line containing chr)
Line 10: Line 10:
  
 
A trivial example shall demonstrate the usage of <syntaxhighlight lang="pascal" enclose="none">chr</syntaxhighlight>:
 
A trivial example shall demonstrate the usage of <syntaxhighlight lang="pascal" enclose="none">chr</syntaxhighlight>:
<syntaxhighlight lang="pascal">
+
<syntaxhighlight lang="pascal" highlight="10">
 
type
 
type
 
// modern Latin alphabet has 26 letters
 
// modern Latin alphabet has 26 letters

Revision as of 19:06, 12 March 2018

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.