Difference between revisions of "Chr"
From Lazarus wiki
Jump to navigationJump to search (New page: Function chr returns the char which has ASCII value X Declaration Function chr (X : byte) : Char; See also: * ord) |
|||
(6 intermediate revisions by 4 users not shown) | |||
Line 1: | Line 1: | ||
− | + | {{Chr}} | |
− | + | 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: | |
+ | <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" inline>chr</syntaxhighlight> is better style. | ||
+ | A trivial example shall demonstrate the usage of <syntaxhighlight lang="pascal" inline>chr</syntaxhighlight>: | ||
+ | <syntaxhighlight lang="pascal" highlight="10"> | ||
+ | type | ||
+ | // modern Latin alphabet has 26 letters | ||
+ | latinAlphabetCharacterIndex = 0..25; | ||
− | + | {$push} | |
− | * [[ord]] | + | // 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> | ||
+ | |||
+ | [[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 == | ||
+ | * {{Doc|package=RTL|unit=system|identifier=chr|text=<syntaxhighlight lang="pascal" inline>chr</syntaxhighlight>}} in the system unit reference | ||
+ | * [[Ord|<syntaxhighlight lang="pascal" inline>ord</syntaxhighlight>]] does the reverse operation. | ||
+ | |||
+ | [[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
.