Difference between revisions of "Chr"

From Lazarus wiki
Jump to navigationJump to search
m
(review)
Line 1: Line 1:
 
{{Chr}}
 
{{Chr}}
  
Function chr returns the [[Char|char]] which has [[ASCII]] value X
+
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 signature reads:
 +
<syntaxhighlight lang="pascal">
 +
function chr(b: byte): char;
 +
</syntaxhighlight>
 +
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.
  
Declaration
+
A trivial example shall demonstrate the usage of <syntaxhighlight lang="pascal" enclose="none">chr</syntaxhighlight>:
    Function chr (X : byte) : Char;  
+
<syntaxhighlight lang="pascal">
 +
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}
 +
</syntaxhighlight>
  
See also:
+
[[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>].
* [[Ord|ord]]
+
 
 +
== see also ==
 +
* {{Doc|package=RTL|unit=system|identifier=chr|text=<syntaxhighlight lang="pascal" enclose="none">chr</syntaxhighlight>}} in the system unit reference
 +
* [[Ord|<syntaxhighlight lang="pascal" enclose="none">ord</syntaxhighlight>]] does the reverse operation.
  
 
[[Category:Pascal]]
 
[[Category:Pascal]]

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.