Difference between revisions of "Chr"

From Lazarus wiki
Jump to navigationJump to search
m (highlight line containing chr)
 
Line 1: Line 1:
 
{{Chr}}
 
{{Chr}}
  
The function <syntaxhighlight lang="pascal" enclose="none">chr</syntaxhighlight> returns the [[Char|<syntaxhighlight lang="pascal" enclose="none">char</syntaxhighlight>]] which has [[ASCII]] value <syntaxhighlight lang="pascal" enclose="none">b</syntaxhighlight>.
+
The function <syntaxhighlight lang="pascal" inline>chr</syntaxhighlight> returns the [[Char|<syntaxhighlight lang="pascal" inline>char</syntaxhighlight>]] which has [[ASCII]] value <syntaxhighlight lang="pascal" inline>b</syntaxhighlight>.
 
The signature reads:
 
The signature reads:
 
<syntaxhighlight lang="pascal">
 
<syntaxhighlight lang="pascal">
Line 7: Line 7:
 
</syntaxhighlight>
 
</syntaxhighlight>
 
The function is used and necessary because of Pascal's strong type safety.
 
The function is used and necessary because of Pascal's strong type safety.
With the advent of [[Typecast|typecasts]] a general method became available, though using <syntaxhighlight lang="pascal" enclose="none">chr</syntaxhighlight> is better style.
+
With the advent of [[Typecast|typecasts]] a general method became available, though using <syntaxhighlight lang="pascal" inline>chr</syntaxhighlight> is better style.
  
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" inline>chr</syntaxhighlight>:
 
<syntaxhighlight lang="pascal" highlight="10">
 
<syntaxhighlight lang="pascal" highlight="10">
 
type
 
type
Line 25: Line 25:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
[[FPC]] has implemented <syntaxhighlight lang="pascal" enclose="none">chr</syntaxhighlight> as a compiler intrinsic [https://svn.freepascal.org/cgi-bin/viewvc.cgi/tags/release_3_0_4/compiler/ninl.pas?revision=37113&view=markup#l2221 <code>in_chr_byte</code>].
+
[[FPC]] has implemented <syntaxhighlight lang="pascal" inline>chr</syntaxhighlight> as a compiler intrinsic [https://svn.freepascal.org/cgi-bin/viewvc.cgi/tags/release_3_0_4/compiler/ninl.pas?revision=37113&view=markup#l2221 <code>in_chr_byte</code>].
  
 
== see also ==
 
== see also ==
* {{Doc|package=RTL|unit=system|identifier=chr|text=<syntaxhighlight lang="pascal" enclose="none">chr</syntaxhighlight>}} in the system unit reference
+
* {{Doc|package=RTL|unit=system|identifier=chr|text=<syntaxhighlight lang="pascal" inline>chr</syntaxhighlight>}} in the system unit reference
* [[Ord|<syntaxhighlight lang="pascal" enclose="none">ord</syntaxhighlight>]] does the reverse operation.
+
* [[Ord|<syntaxhighlight lang="pascal" inline>ord</syntaxhighlight>]] does the reverse operation.
  
 
[[Category:Pascal]]
 
[[Category:Pascal]]

Latest revision as of 17:21, 6 August 2022

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.