Difference between revisions of "Chr"
From Lazarus wiki
Jump to navigationJump to search (review) |
|||
(One intermediate revision by the same user not shown) | |||
Line 1: | Line 1: | ||
{{Chr}} | {{Chr}} | ||
− | The function <syntaxhighlight lang="pascal" | + | 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" | + | 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" | + | A trivial example shall demonstrate the usage of <syntaxhighlight lang="pascal" inline>chr</syntaxhighlight>: |
− | <syntaxhighlight lang="pascal"> | + | <syntaxhighlight lang="pascal" highlight="10"> |
type | type | ||
// modern Latin alphabet has 26 letters | // modern Latin alphabet has 26 letters | ||
Line 25: | Line 25: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | [[FPC]] has implemented <syntaxhighlight lang="pascal" | + | [[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" | + | * {{Doc|package=RTL|unit=system|identifier=chr|text=<syntaxhighlight lang="pascal" inline>chr</syntaxhighlight>}} in the system unit reference |
− | * [[Ord|<syntaxhighlight lang="pascal" | + | * [[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
.