Chr: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: Chr
| Old | New | Line | |
|---|---|---|---|
| 1 | + | The function `chr` returns the [[Char|char]] which has [[ASCII]] value `b`. The signature reads: | |
| 2 | + | ||
| 3 | + | ``` pascal | |
| 4 | + | function chr(b: byte): char; | |
| 5 | + | ``` | |
| 6 | + | ||
| 7 | + | 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 `chr` is better style. | |
| 8 | + | ||
| 9 | + | A trivial example shall demonstrate the usage of `chr`: | |
| 10 | + | ||
| 11 | + | ``` pascal | |
| 12 | + | type | |
| 13 | + | // modern Latin alphabet has 26 letters | |
| 14 | + | latinAlphabetCharacterIndex = 0..25; | |
| 15 | + | ||
| 16 | + | {$push} | |
| 17 | + | // let compiler generate code ensuring n >= 0 and n < 26 | |
| 18 | + | {$rangeChecks on} | |
| 19 | + | function getChrInLatinAlphabet(const n: latinAlphabetCharacterIndex): char; | |
| 20 | + | begin | |
| 21 | + | getChrInLatinAlphabet := chr(ord('A') + n); | |
| 22 | + | end; | |
| 23 | + | {$pop} | |
| 24 | + | ``` | |
| 25 | + | ||
| 26 | + | [[FPC]] has implemented `chr` as a compiler intrinsic [`in_chr_byte`](https://svn.freepascal.org/cgi-bin/viewvc.cgi/tags/release_3_0_4/compiler/ninl.pas?revision=37113&view=markup#l2221). | |
| 27 | + | ||
| 28 | + | ## see also | |
| 29 | + | ||
| 30 | + | - [`chr`](https://www.freepascal.org/docs-html/rtl/system/chr.html) in the system unit reference | |
| 31 | + | - [[Ord|ord]] does the reverse operation. | |
| 32 | + | ||
| 33 | + | --- | |
| 34 | + | *From the Free Pascal wiki, page "Chr", as it stood on 2022-08-06 after a change by Kai Burghardt, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).* | |
| 35 | + | Category: Pascal |
