Character codes: Difference between revisions
Content added Content deleted
(Added Bash / Unix Shell Implementation) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 16: | Line 16: | ||
=={{header|11l}}== |
=={{header|11l}}== |
||
< |
<syntaxhighlight lang=11l>print(‘a’.code) // prints "97" |
||
print(Char(code' 97)) // prints "a"</ |
print(Char(code' 97)) // prints "a"</syntaxhighlight> |
||
=={{header|360 Assembly}}== |
=={{header|360 Assembly}}== |
||
Line 23: | Line 23: | ||
In EBCDIC, the character 'a' (lowercase letter A) has a code of 129 in decimal and '81'x in hexadecimal. |
In EBCDIC, the character 'a' (lowercase letter A) has a code of 129 in decimal and '81'x in hexadecimal. |
||
To perform conversion, we use IC (insert character) and STC (store character) opcodes. |
To perform conversion, we use IC (insert character) and STC (store character) opcodes. |
||
< |
<syntaxhighlight lang=360asm>* Character codes EBCDIC 15/02/2017 |
||
CHARCODE CSECT |
CHARCODE CSECT |
||
USING CHARCODE,R13 base register |
USING CHARCODE,R13 base register |
||
Line 56: | Line 56: | ||
CHAR DS CL1 |
CHAR DS CL1 |
||
YREGS |
YREGS |
||
END CHARCODE</ |
END CHARCODE</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 66: | Line 66: | ||
The printing routine only understands ASCII characters as codes anyway, so the "given a code produce its character" part is trivial. |
The printing routine only understands ASCII characters as codes anyway, so the "given a code produce its character" part is trivial. |
||
The <code>PrintChar</code> routine is omitted for brevity. It converts the two cursor variables to a FIX layer address and outputs the character using the NEOGEO's FIX layer (the layer where text is displayed). Characters are stored in ROM and arranged in ASCII order. |
The <code>PrintChar</code> routine is omitted for brevity. It converts the two cursor variables to a FIX layer address and outputs the character using the NEOGEO's FIX layer (the layer where text is displayed). Characters are stored in ROM and arranged in ASCII order. |
||
< |
<syntaxhighlight lang=68000devpac> JSR ResetCoords ;RESET TYPING CURSOR |
||
MOVE.B #'A',D1 |
MOVE.B #'A',D1 |
||
Line 139: | Line 139: | ||
MOVE.B D1,D0 ;store in low word |
MOVE.B D1,D0 ;store in low word |
||
popWord D1 |
popWord D1 |
||
rts</ |
rts</syntaxhighlight> |
||
Output can be seen [https://ibb.co/ngtDXpq here.] |
Output can be seen [https://ibb.co/ngtDXpq here.] |
||
=={{header|AArch64 Assembly}}== |
=={{header|AArch64 Assembly}}== |
||
{{works with|as|Raspberry Pi 3B version Buster 64 bits}} |
{{works with|as|Raspberry Pi 3B version Buster 64 bits}} |
||
< |
<syntaxhighlight lang=AArch64 Assembly> |
||
/* ARM assembly AARCH64 Raspberry PI 3B */ |
/* ARM assembly AARCH64 Raspberry PI 3B */ |
||
/* program character64.s */ |
/* program character64.s */ |
||
Line 204: | Line 204: | ||
/* for this file see task include a file in language AArch64 assembly */ |
/* for this file see task include a file in language AArch64 assembly */ |
||
.include "../includeARM64.inc" |
.include "../includeARM64.inc" |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|ABAP}}== |
=={{header|ABAP}}== |
||
In ABAP you must first cast the character to a byte field and back to a number in order to get its ASCII value. |
In ABAP you must first cast the character to a byte field and back to a number in order to get its ASCII value. |
||
< |
<syntaxhighlight lang=ABAP>report zcharcode |
||
data: c value 'A', n type i. |
data: c value 'A', n type i. |
||
field-symbols <n> type x. |
field-symbols <n> type x. |
||
Line 214: | Line 214: | ||
assign c to <n> casting. |
assign c to <n> casting. |
||
move <n> to n. |
move <n> to n. |
||
write: c, '=', n left-justified.</ |
write: c, '=', n left-justified.</syntaxhighlight> |
||
{{Out}}<pre>A = 65</pre> |
{{Out}}<pre>A = 65</pre> |
||
=={{header|ACL2}}== |
=={{header|ACL2}}== |
||
Similar to Common Lisp: |
Similar to Common Lisp: |
||
< |
<syntaxhighlight lang=Lisp>(cw "~x0" (char-code #\a)) |
||
(cw "~x0" (code-char 97))</ |
(cw "~x0" (code-char 97))</syntaxhighlight> |
||
=={{header|Action!}}== |
=={{header|Action!}}== |
||
< |
<syntaxhighlight lang=Action!>PROC Main() |
||
CHAR c=['a] |
CHAR c=['a] |
||
BYTE b=[97] |
BYTE b=[97] |
||
Line 229: | Line 229: | ||
Put(c) Put('=) PrintBE(c) |
Put(c) Put('=) PrintBE(c) |
||
PrintB(b) Put('=) Put(b) |
PrintB(b) Put('=) Put(b) |
||
RETURN</ |
RETURN</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Character_codes.png Screenshot from Atari 8-bit computer] |
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Character_codes.png Screenshot from Atari 8-bit computer] |
||
Line 239: | Line 239: | ||
=={{header|ActionScript}}== |
=={{header|ActionScript}}== |
||
In ActionScript, you cannot take the character code of a character directly. Instead you must create a string and call charCodeAt with the character's position in the string as a parameter. |
In ActionScript, you cannot take the character code of a character directly. Instead you must create a string and call charCodeAt with the character's position in the string as a parameter. |
||
< |
<syntaxhighlight lang=ActionScipt>trace(String.fromCharCode(97)); //prints 'a' |
||
trace("a".charCodeAt(0));//prints '97'</ |
trace("a".charCodeAt(0));//prints '97'</syntaxhighlight> |
||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
< |
<syntaxhighlight lang=ada>with Ada.Text_IO; use Ada.Text_IO; |
||
procedure Char_Code is |
procedure Char_Code is |
||
begin |
begin |
||
Put_Line (Character'Val (97) & " =" & Integer'Image (Character'Pos ('a'))); |
Put_Line (Character'Val (97) & " =" & Integer'Image (Character'Pos ('a'))); |
||
end Char_Code;</ |
end Char_Code;</syntaxhighlight> |
||
The predefined language attributes S'Pos and S'Val for every discrete subtype, and Character is such a type, yield the position of a value and value by its position correspondingly. |
The predefined language attributes S'Pos and S'Val for every discrete subtype, and Character is such a type, yield the position of a value and value by its position correspondingly. |
||
{{out}} |
{{out}} |
||
Line 254: | Line 254: | ||
=={{header|Aime}}== |
=={{header|Aime}}== |
||
< |
<syntaxhighlight lang=aime># prints "97" |
||
o_integer('a'); |
o_integer('a'); |
||
o_byte('\n'); |
o_byte('\n'); |
||
# prints "a" |
# prints "a" |
||
o_byte(97); |
o_byte(97); |
||
o_byte('\n');</ |
o_byte('\n');</syntaxhighlight> |
||
=={{header|ALGOL 68}}== |
=={{header|ALGOL 68}}== |
||
In ALGOL 68 the '''format''' $g$ is type aware, hence the type conversion operators '''abs''' & '''repr''' are used to set the type. |
In ALGOL 68 the '''format''' $g$ is type aware, hence the type conversion operators '''abs''' & '''repr''' are used to set the type. |
||
< |
<syntaxhighlight lang=algol68>main:( |
||
printf(($gl$, ABS "a")); # for ASCII this prints "+97" EBCDIC prints "+129" # |
printf(($gl$, ABS "a")); # for ASCII this prints "+97" EBCDIC prints "+129" # |
||
printf(($gl$, REPR 97)) # for ASCII this prints "a"; EBCDIC prints "/" # |
printf(($gl$, REPR 97)) # for ASCII this prints "a"; EBCDIC prints "/" # |
||
)</ |
)</syntaxhighlight> |
||
''Character conversions'' may be available in the ''standard prelude'' so that when |
''Character conversions'' may be available in the ''standard prelude'' so that when |
||
a foreign tape is mounted, the characters will be converted transparently as the tape's |
a foreign tape is mounted, the characters will be converted transparently as the tape's |
||
records are read. |
records are read. |
||
< |
<syntaxhighlight lang=algol68>FILE tape; |
||
INT errno = open(tape, "/dev/tape1", stand out channel) |
INT errno = open(tape, "/dev/tape1", stand out channel) |
||
make conv(tape, ebcdic conv); |
make conv(tape, ebcdic conv); |
||
FOR record DO getf(tape, ( ~ )) OD; ~ # etc ... #</ |
FOR record DO getf(tape, ( ~ )) OD; ~ # etc ... #</syntaxhighlight> |
||
Every '''channel''' has an associated standard character conversion that can be determined |
Every '''channel''' has an associated standard character conversion that can be determined |
||
using the ''stand conv'' query routine and then the conversion applied to a particular |
using the ''stand conv'' query routine and then the conversion applied to a particular |
||
file/tape. eg. |
file/tape. eg. |
||
< |
<syntaxhighlight lang=algol68> make conv(tape, stand conv(stand out channel))</syntaxhighlight> |
||
=={{header|ALGOL W}}== |
=={{header|ALGOL W}}== |
||
< |
<syntaxhighlight lang=algolw>begin |
||
% display the character code of "a" (97 in ASCII) % |
% display the character code of "a" (97 in ASCII) % |
||
write( decode( "a" ) ); |
write( decode( "a" ) ); |
||
% display the character corresponding to 97 ("a" in ASCII) % |
% display the character corresponding to 97 ("a" in ASCII) % |
||
write( code( 97 ) ); |
write( code( 97 ) ); |
||
end.</ |
end.</syntaxhighlight> |
||
=={{header|APL}}== |
=={{header|APL}}== |
||
Line 291: | Line 291: | ||
{{works with|GNU APL}} |
{{works with|GNU APL}} |
||
In GNU APL and Dyalog, <tt>⎕UCS</tt> with an integer returns the corresponding Unicode character: |
In GNU APL and Dyalog, <tt>⎕UCS</tt> with an integer returns the corresponding Unicode character: |
||
< |
<syntaxhighlight lang=apl> ⎕UCS 97 |
||
a</ |
a</syntaxhighlight> |
||
and <tt>⎕UCS</tt> with a character returns the corresponding code: |
and <tt>⎕UCS</tt> with a character returns the corresponding code: |
||
< |
<syntaxhighlight lang=apl> ⎕UCS 'a' |
||
97</ |
97</syntaxhighlight> |
||
Like most things in APL, <tt>⎕UCS</tt> can also be used with an array or with a string (which is an array of characters): |
Like most things in APL, <tt>⎕UCS</tt> can also be used with an array or with a string (which is an array of characters): |
||
< |
<syntaxhighlight lang=apl> ⎕UCS 65 80 76 |
||
APL |
APL |
||
⎕UCS 'Hello, world!' |
⎕UCS 'Hello, world!' |
||
72 101 108 108 111 44 32 119 111 114 108 100 33</ |
72 101 108 108 111 44 32 119 111 114 108 100 33</syntaxhighlight> |
||
=={{header|AppleScript}}== |
=={{header|AppleScript}}== |
||
< |
<syntaxhighlight lang=AppleScript>log(id of "a") |
||
log(id of "aA")</ |
log(id of "aA")</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>(*97*) |
<pre>(*97*) |
||
Line 311: | Line 311: | ||
The converse instruction is <tt>character id</tt> — or either of its synonyms <tt>string id</tt> and <tt>Unicode text id</tt>. Because of a bug admitted to in Apple's AppleScript Language Guide, the expression <tt>text id</tt>, which one might expect to work, can't be used. |
The converse instruction is <tt>character id</tt> — or either of its synonyms <tt>string id</tt> and <tt>Unicode text id</tt>. Because of a bug admitted to in Apple's AppleScript Language Guide, the expression <tt>text id</tt>, which one might expect to work, can't be used. |
||
< |
<syntaxhighlight lang=applescript>character id 97 |
||
--> "a" |
--> "a" |
||
Line 321: | Line 321: | ||
Unicode text id {72, 101, 108, 108, 111, 33} |
Unicode text id {72, 101, 108, 108, 111, 33} |
||
--> "Hello!"</ |
--> "Hello!"</syntaxhighlight> |
||
=={{header|ARM Assembly}}== |
=={{header|ARM Assembly}}== |
||
{{works with|as|Raspberry Pi}} |
{{works with|as|Raspberry Pi}} |
||
< |
<syntaxhighlight lang=ARM Assembly> |
||
/* ARM assembly Raspberry PI */ |
/* ARM assembly Raspberry PI */ |
||
/* program character.s */ |
/* program character.s */ |
||
Line 452: | Line 452: | ||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Arturo}}== |
=={{header|Arturo}}== |
||
< |
<syntaxhighlight lang=rebol>print to :integer first "a" |
||
print to :integer `a` |
print to :integer `a` |
||
print to :char 97</ |
print to :char 97</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 467: | Line 467: | ||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
< |
<syntaxhighlight lang=AutoHotkey>MsgBox % Chr(97) |
||
MsgBox % Asc("a")</ |
MsgBox % Asc("a")</syntaxhighlight> |
||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
Line 474: | Line 474: | ||
but a function that does so can be easily built using an associative array (where the keys are the characters). |
but a function that does so can be easily built using an associative array (where the keys are the characters). |
||
The opposite can be done using <tt>printf</tt> (or <tt>sprintf</tt>) with <tt>%c</tt> |
The opposite can be done using <tt>printf</tt> (or <tt>sprintf</tt>) with <tt>%c</tt> |
||
< |
<syntaxhighlight lang=awk>function ord(c) |
||
{ |
{ |
||
return chmap[c] |
return chmap[c] |
||
Line 486: | Line 486: | ||
s = sprintf("%c%c", 97, 98) |
s = sprintf("%c%c", 97, 98) |
||
print s |
print s |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Axe}}== |
=={{header|Axe}}== |
||
< |
<syntaxhighlight lang=axe>Disp 'a'▶Dec,i |
||
Disp 97▶Char,i</ |
Disp 97▶Char,i</syntaxhighlight> |
||
=={{header|Babel}}== |
=={{header|Babel}}== |
||
< |
<syntaxhighlight lang=babel>'abcdefg' str2ar |
||
{%d nl <<} eachar</ |
{%d nl <<} eachar</syntaxhighlight> |
||
{{Out}}<pre> |
{{Out}}<pre> |
||
Line 507: | Line 507: | ||
</pre> |
</pre> |
||
< |
<syntaxhighlight lang=babel>(98 97 98 101 108) ls2lf ar2str nl << |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
babel |
babel |
||
Line 514: | Line 514: | ||
=={{header|BASIC}}== |
=={{header|BASIC}}== |
||
{{works with|QuickBasic|4.5}} |
{{works with|QuickBasic|4.5}} |
||
< |
<syntaxhighlight lang=qbasic>charCode = 97 |
||
char = "a" |
char = "a" |
||
PRINT CHR$(charCode) 'prints a |
PRINT CHR$(charCode) 'prints a |
||
PRINT ASC(char) 'prints 97</ |
PRINT ASC(char) 'prints 97</syntaxhighlight> |
||
On the ZX Spectrum string variable names must be a single letter but numeric variables can be multiple characters: |
On the ZX Spectrum string variable names must be a single letter but numeric variables can be multiple characters: |
||
{{works with|ZX Spectrum Basic}} |
{{works with|ZX Spectrum Basic}} |
||
< |
<syntaxhighlight lang=zxbasic>10 LET c = 97: REM c is a character code |
||
20 LET d$ = "b": REM d$ holds the character |
20 LET d$ = "b": REM d$ holds the character |
||
30 PRINT CHR$(c): REM this prints a |
30 PRINT CHR$(c): REM this prints a |
||
40 PRINT CODE(d$): REM this prints 98</ |
40 PRINT CODE(d$): REM this prints 98</syntaxhighlight> |
||
==={{header|Applesoft BASIC}}=== |
==={{header|Applesoft BASIC}}=== |
||
CHR$(97) is used in place of "a" because on the older model Apple II, lower case is difficult to input. |
CHR$(97) is used in place of "a" because on the older model Apple II, lower case is difficult to input. |
||
< |
<syntaxhighlight lang=qbasic>?CHR$(97)"="ASC(CHR$(97))</syntaxhighlight> |
||
{{Out}}<pre>a=97</pre> |
{{Out}}<pre>a=97</pre> |
||
Line 535: | Line 535: | ||
==={{header|BaCon}}=== |
==={{header|BaCon}}=== |
||
< |
<syntaxhighlight lang=qbasic>' ASCII |
||
c$ = "$" |
c$ = "$" |
||
PRINT c$, ": ", ASC(c$) |
PRINT c$, ": ", ASC(c$) |
||
Line 541: | Line 541: | ||
' UTF-8 |
' UTF-8 |
||
uc$ = "€" |
uc$ = "€" |
||
PRINT uc$, ": ", UCS(uc$), ", ", UCS(c$)</ |
PRINT uc$, ": ", UCS(uc$), ", ", UCS(c$)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 550: | Line 550: | ||
==={{header|Commodore BASIC}}=== |
==={{header|Commodore BASIC}}=== |
||
Commodore BASIC uses PETSCII code for its character set. |
Commodore BASIC uses PETSCII code for its character set. |
||
< |
<syntaxhighlight lang=gwbasic>10 CH = 65: REM IN PETSCII CODE FOR 'A' IS 65 |
||
20 D$ = "B": REM D$ HOLDS THE CHARACTER 'B' |
20 D$ = "B": REM D$ HOLDS THE CHARACTER 'B' |
||
30 PRINT CHR$(CH): REM THIS PRINTS 'A' |
30 PRINT CHR$(CH): REM THIS PRINTS 'A' |
||
40 PRINT ASC(D$): REM THIS PRINTS 66</ |
40 PRINT ASC(D$): REM THIS PRINTS 66</syntaxhighlight> |
||
{{Out}}<pre>A |
{{Out}}<pre>A |
||
66</pre> |
66</pre> |
||
==={{header|IS-BASIC}}=== |
==={{header|IS-BASIC}}=== |
||
< |
<syntaxhighlight lang=IS-BASIC>100 PRINT ORD("A") |
||
110 PRINT CHR$(65)</ |
110 PRINT CHR$(65)</syntaxhighlight> |
||
==={{header|QBasic}}=== |
==={{header|QBasic}}=== |
||
{{works with|FreeBASIC}} |
{{works with|FreeBASIC}} |
||
{{works with|Run BASIC}} |
{{works with|Run BASIC}} |
||
< |
<syntaxhighlight lang=QBasic>PRINT "a - > "; ASC("a") |
||
PRINT "98 -> "; CHR$(98)</ |
PRINT "98 -> "; CHR$(98)</syntaxhighlight> |
||
==={{header|Sinclair ZX81 BASIC}}=== |
==={{header|Sinclair ZX81 BASIC}}=== |
||
< |
<syntaxhighlight lang=basic>10 REM THE ZX81 USES ITS OWN NON-ASCII CHARACTER SET |
||
20 REM WHICH DOES NOT INCLUDE LOWER-CASE LETTERS |
20 REM WHICH DOES NOT INCLUDE LOWER-CASE LETTERS |
||
30 PRINT CODE "A" |
30 PRINT CODE "A" |
||
40 PRINT CHR$ 38</ |
40 PRINT CHR$ 38</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>38 |
<pre>38 |
||
Line 577: | Line 577: | ||
==={{header|True BASIC}}=== |
==={{header|True BASIC}}=== |
||
< |
<syntaxhighlight lang=qbasic>PRINT "a - > "; ord("a") |
||
PRINT "98 -> "; chr$(98) |
PRINT "98 -> "; chr$(98) |
||
END</ |
END</syntaxhighlight> |
||
==={{header|Yabasic}}=== |
==={{header|Yabasic}}=== |
||
< |
<syntaxhighlight lang=yabasic>print "a - > ", asc("a") |
||
print "98 -> ", chr$(98)</ |
print "98 -> ", chr$(98)</syntaxhighlight> |
||
=={{header|BASIC256}}== |
=={{header|BASIC256}}== |
||
< |
<syntaxhighlight lang=freebasic># ASCII char |
||
charCode = 97 |
charCode = 97 |
||
char$ = "a" |
char$ = "a" |
||
Line 597: | Line 597: | ||
char$ = "π" |
char$ = "π" |
||
print chr(960) #prints π |
print chr(960) #prints π |
||
print asc("π") #prints 960</ |
print asc("π") #prints 960</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>a |
<pre>a |
||
Line 606: | Line 606: | ||
=={{header|Batch File}}== |
=={{header|Batch File}}== |
||
< |
<syntaxhighlight lang=dos> |
||
@echo off |
@echo off |
||
Line 640: | Line 640: | ||
echo %=exitcodeAscii% |
echo %=exitcodeAscii% |
||
exit /b |
exit /b |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{in}} |
{{in}} |
||
<pre> |
<pre> |
||
Line 653: | Line 653: | ||
=={{header|BBC BASIC}}== |
=={{header|BBC BASIC}}== |
||
< |
<syntaxhighlight lang=bbcbasic> charCode = 97 |
||
char$ = "a" |
char$ = "a" |
||
PRINT CHR$(charCode) : REM prints a |
PRINT CHR$(charCode) : REM prints a |
||
PRINT ASC(char$) : REM prints 97</ |
PRINT ASC(char$) : REM prints 97</syntaxhighlight> |
||
=={{header|Befunge}}== |
=={{header|Befunge}}== |
||
The instruction <tt>.</tt> will output as an integer. <tt>,</tt> will output as ASCII character. |
The instruction <tt>.</tt> will output as an integer. <tt>,</tt> will output as ASCII character. |
||
< |
<syntaxhighlight lang=befunge>"a". 99*44*+, @</syntaxhighlight> |
||
=={{header|BQN}}== |
=={{header|BQN}}== |
||
BQN's character arithmetic makes it easy to convert between numbers and characters. Since arithmetic generalizes to arrays, the same function works for both integers and arrays. Here, only the conversion from number to character is defined, since it can be automatically inverted with Undo (<code>⁼</code>): the inverse simply subtracts <code>@</code>. |
BQN's character arithmetic makes it easy to convert between numbers and characters. Since arithmetic generalizes to arrays, the same function works for both integers and arrays. Here, only the conversion from number to character is defined, since it can be automatically inverted with Undo (<code>⁼</code>): the inverse simply subtracts <code>@</code>. |
||
< |
<syntaxhighlight lang=bqn> FromCharCode ← @⊸+ |
||
@⊸+ |
@⊸+ |
||
FromCharCode 97 |
FromCharCode 97 |
||
Line 672: | Line 672: | ||
"aC~" |
"aC~" |
||
FromCharCode⁼ 'a' |
FromCharCode⁼ 'a' |
||
97</ |
97</syntaxhighlight> |
||
=={{header|Bracmat}}== |
=={{header|Bracmat}}== |
||
< |
<syntaxhighlight lang=bracmat>( put |
||
$ ( str |
$ ( str |
||
$ ( "\nLatin a |
$ ( "\nLatin a |
||
Line 695: | Line 695: | ||
) |
) |
||
) |
) |
||
)</ |
)</syntaxhighlight> |
||
{{Out}}<pre>Latin a |
{{Out}}<pre>Latin a |
||
ISO-9959-1: 97 = a |
ISO-9959-1: 97 = a |
||
Line 704: | Line 704: | ||
<tt>char</tt> is already an integer type in C, and it gets automatically promoted to <tt>int</tt>. So you can use a character where you would otherwise use an integer. Conversely, you can use an integer where you would normally use a character, except you may need to cast it, as <tt>char</tt> is smaller. |
<tt>char</tt> is already an integer type in C, and it gets automatically promoted to <tt>int</tt>. So you can use a character where you would otherwise use an integer. Conversely, you can use an integer where you would normally use a character, except you may need to cast it, as <tt>char</tt> is smaller. |
||
< |
<syntaxhighlight lang=c>#include <stdio.h> |
||
int main() { |
int main() { |
||
Line 710: | Line 710: | ||
printf("%c\n", 97); /* prints "a"; we don't have to cast because printf is type agnostic */ |
printf("%c\n", 97); /* prints "a"; we don't have to cast because printf is type agnostic */ |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
=={{header|C sharp|C#}}== |
=={{header|C sharp|C#}}== |
||
C# represents strings and characters internally as Unicode, |
C# represents strings and characters internally as Unicode, |
||
so casting a char to an int returns its Unicode character encoding. |
so casting a char to an int returns its Unicode character encoding. |
||
< |
<syntaxhighlight lang=csharp>using System; |
||
namespace RosettaCode.CharacterCode |
namespace RosettaCode.CharacterCode |
||
Line 727: | Line 727: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|C++}}== |
=={{header|C++}}== |
||
Line 733: | Line 733: | ||
In this case, the output operator <tt><<</tt> is overloaded to handle integer (outputs the decimal representation) and character (outputs just the character) types differently, so we need to cast it in both cases. |
In this case, the output operator <tt><<</tt> is overloaded to handle integer (outputs the decimal representation) and character (outputs just the character) types differently, so we need to cast it in both cases. |
||
< |
<syntaxhighlight lang=cpp>#include <iostream> |
||
int main() { |
int main() { |
||
Line 739: | Line 739: | ||
std::cout << (char)97 << std::endl; // prints "a" |
std::cout << (char)97 << std::endl; // prints "a" |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Clojure}}== |
=={{header|Clojure}}== |
||
< |
<syntaxhighlight lang=clojure>(print (int \a)) ; prints "97" |
||
(print (char 97)) ; prints \a |
(print (char 97)) ; prints \a |
||
Line 751: | Line 751: | ||
; use String because char in Java can't represent characters outside Basic Multilingual Plane |
; use String because char in Java can't represent characters outside Basic Multilingual Plane |
||
(print (.codePointAt "𝅘𝅥𝅮" 0)) ; prints 119136 |
(print (.codePointAt "𝅘𝅥𝅮" 0)) ; prints 119136 |
||
(print (String. (int-array 1 119136) 0 1)) ; prints 𝅘𝅥𝅮</ |
(print (String. (int-array 1 119136) 0 1)) ; prints 𝅘𝅥𝅮</syntaxhighlight> |
||
=={{header|CLU}}== |
=={{header|CLU}}== |
||
< |
<syntaxhighlight lang=clu>start_up = proc () |
||
po: stream := stream$primary_output() |
po: stream := stream$primary_output() |
||
Line 764: | Line 764: | ||
% To turn an integer into a character code, use char$i2c |
% To turn an integer into a character code, use char$i2c |
||
stream$putc(po, char$i2c( 97 ) ); % prints 'a' |
stream$putc(po, char$i2c( 97 ) ); % prints 'a' |
||
end start_up</ |
end start_up</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>97 |
<pre>97 |
||
Line 772: | Line 772: | ||
Tested with GnuCOBOL on an ASCII based GNU/Linux system. |
Tested with GnuCOBOL on an ASCII based GNU/Linux system. |
||
Running this code on EBCDIC native hardware would display a control code and 000000093. |
Running this code on EBCDIC native hardware would display a control code and 000000093. |
||
< |
<syntaxhighlight lang=COBOL> identification division. |
||
program-id. character-codes. |
program-id. character-codes. |
||
remarks. COBOL is an ordinal language, first is 1. |
remarks. COBOL is an ordinal language, first is 1. |
||
Line 780: | Line 780: | ||
display function ord('*') |
display function ord('*') |
||
goback. |
goback. |
||
end program character-codes.</ |
end program character-codes.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 789: | Line 789: | ||
=={{header|CoffeeScript}}== |
=={{header|CoffeeScript}}== |
||
CoffeeScript transcompiles to JavaScript, so it uses the JS standard library. |
CoffeeScript transcompiles to JavaScript, so it uses the JS standard library. |
||
< |
<syntaxhighlight lang=coffeescript>console.log 'a'.charCodeAt 0 # 97 |
||
console.log String.fromCharCode 97 # a</ |
console.log String.fromCharCode 97 # a</syntaxhighlight> |
||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |
||
< |
<syntaxhighlight lang=lisp>(princ (char-code #\a)) ; prints "97" |
||
(princ (code-char 97)) ; prints "a"</ |
(princ (code-char 97)) ; prints "a"</syntaxhighlight> |
||
=={{header|Component Pascal}}== |
=={{header|Component Pascal}}== |
||
BlackBox Component Builder |
BlackBox Component Builder |
||
< |
<syntaxhighlight lang=oberon2>PROCEDURE CharCodes*; |
||
VAR |
VAR |
||
c : CHAR; |
c : CHAR; |
||
Line 806: | Line 806: | ||
c := CHR(3A9H); |
c := CHR(3A9H); |
||
StdLog.Char(c);StdLog.String(":> ");StdLog.Int(ORD(c));StdLog.Ln |
StdLog.Char(c);StdLog.String(":> ");StdLog.Int(ORD(c));StdLog.Ln |
||
END CharCodes;</ |
END CharCodes;</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>A:> 65 |
<pre>A:> 65 |
||
Line 812: | Line 812: | ||
=={{header|D}}== |
=={{header|D}}== |
||
< |
<syntaxhighlight lang=d>void main() { |
||
import std.stdio, std.utf; |
import std.stdio, std.utf; |
||
Line 823: | Line 823: | ||
// 'index' has moved to next character input position. |
// 'index' has moved to next character input position. |
||
assert(index == 1); |
assert(index == 1); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>97</pre> |
<pre>97</pre> |
||
Line 829: | Line 829: | ||
=={{header|Dc}}== |
=={{header|Dc}}== |
||
A dc program cannot look into strings. But it can convert numeric values into single char strings or print numeric codes directly: |
A dc program cannot look into strings. But it can convert numeric values into single char strings or print numeric codes directly: |
||
<lang |
<syntaxhighlight lang=dc>97P</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>a</pre> |
<pre>a</pre> |
||
Line 835: | Line 835: | ||
=={{header|Delphi}}== |
=={{header|Delphi}}== |
||
Example from Studio 2006. |
Example from Studio 2006. |
||
< |
<syntaxhighlight lang=delphi>program Project1; |
||
{$APPTYPE CONSOLE} |
{$APPTYPE CONSOLE} |
||
Line 853: | Line 853: | ||
Readln; |
Readln; |
||
end.</ |
end.</syntaxhighlight> |
||
=={{header|Dyalect}}== |
=={{header|Dyalect}}== |
||
< |
<syntaxhighlight lang=dyalect>print('a'.Order()) |
||
print(Char(97))</ |
print(Char(97))</syntaxhighlight> |
||
=={{header|DWScript}}== |
=={{header|DWScript}}== |
||
< |
<syntaxhighlight lang=delphi>PrintLn(Ord('a')); |
||
PrintLn(Chr(97));</ |
PrintLn(Chr(97));</syntaxhighlight> |
||
=={{header|E}}== |
=={{header|E}}== |
||
< |
<syntaxhighlight lang=e>? 'a'.asInteger() |
||
# value: 97 |
# value: 97 |
||
? <import:java.lang.makeCharacter>.asChar(97) |
? <import:java.lang.makeCharacter>.asChar(97) |
||
# value: 'a'</ |
# value: 'a'</syntaxhighlight> |
||
=={{header|EasyLang}}== |
=={{header|EasyLang}}== |
||
<lang>print strcode "a" |
<syntaxhighlight lang=text>print strcode "a" |
||
print strchar 97</ |
print strchar 97</syntaxhighlight> |
||
=={{header|Eiffel}}== |
=={{header|Eiffel}}== |
||
All characters are of the type CHARACTER_8 (ASCII encoding) or CHARACTER_32 (Unicode encoding). CHARACTER is a synonym for either of these two (depending on the compiler option). Characters can be assigned using character literals (a single character enclosed in single quotes) or code value notation (of the form '%/value/' where value is an integer literal of any of the recognized forms). |
All characters are of the type CHARACTER_8 (ASCII encoding) or CHARACTER_32 (Unicode encoding). CHARACTER is a synonym for either of these two (depending on the compiler option). Characters can be assigned using character literals (a single character enclosed in single quotes) or code value notation (of the form '%/value/' where value is an integer literal of any of the recognized forms). |
||
< |
<syntaxhighlight lang=eiffel> |
||
class |
class |
||
APPLICATION |
APPLICATION |
||
Line 905: | Line 905: | ||
end |
end |
||
end |
end |
||
</syntaxhighlight> |
|||
</lang> |
|||
Limitations: There is no "put_character_32" feature for standard io (FILE class), so there appears to be no way to print Unicode characters. |
Limitations: There is no "put_character_32" feature for standard io (FILE class), so there appears to be no way to print Unicode characters. |
||
Line 911: | Line 911: | ||
=={{header|Elena}}== |
=={{header|Elena}}== |
||
ELENA 4.x : |
ELENA 4.x : |
||
< |
<syntaxhighlight lang=elena>import extensions; |
||
public program() |
public program() |
||
Line 919: | Line 919: | ||
console.printLine:ch; |
console.printLine:ch; |
||
console.printLine(ch.toInt()) |
console.printLine(ch.toInt()) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 928: | Line 928: | ||
=={{header|Elixir}}== |
=={{header|Elixir}}== |
||
A String in Elixir is a UTF-8 encoded binary. |
A String in Elixir is a UTF-8 encoded binary. |
||
< |
<syntaxhighlight lang=elixir>iex(1)> code = ?a |
||
97 |
97 |
||
iex(2)> to_string([code]) |
iex(2)> to_string([code]) |
||
"a"</ |
"a"</syntaxhighlight> |
||
=={{header|Emacs Lisp}}== |
=={{header|Emacs Lisp}}== |
||
< |
<syntaxhighlight lang=Lisp>(string-to-char "a") ;=> 97 |
||
(format "%c" 97) ;=> "a"</ |
(format "%c" 97) ;=> "a"</syntaxhighlight> |
||
=={{header|Erlang}}== |
=={{header|Erlang}}== |
||
In Erlang, lists and strings are the same, only the representation changes. Thus: |
In Erlang, lists and strings are the same, only the representation changes. Thus: |
||
< |
<syntaxhighlight lang=erlang>1> F = fun([X]) -> X end. |
||
#Fun<erl_eval.6.13229925> |
#Fun<erl_eval.6.13229925> |
||
2> F("a"). |
2> F("a"). |
||
97</ |
97</syntaxhighlight> |
||
If entered manually, one can also get ASCII codes by prefixing characters with <tt>$</tt>: |
If entered manually, one can also get ASCII codes by prefixing characters with <tt>$</tt>: |
||
< |
<syntaxhighlight lang=erlang>3> $a. |
||
97</ |
97</syntaxhighlight> |
||
Unicode is fully supported since release R13A only. |
Unicode is fully supported since release R13A only. |
||
=={{header|Euphoria}}== |
=={{header|Euphoria}}== |
||
< |
<syntaxhighlight lang=Euphoria>printf(1,"%d\n", 'a') -- prints "97" |
||
printf(1,"%s\n", 97) -- prints "a"</ |
printf(1,"%s\n", 97) -- prints "a"</syntaxhighlight> |
||
=={{header|F Sharp|F#}}== |
=={{header|F Sharp|F#}}== |
||
< |
<syntaxhighlight lang=fsharp>let c = 'A' |
||
let n = 65 |
let n = 65 |
||
printfn "%d" (int c) |
printfn "%d" (int c) |
||
printfn "%c" (char n)</ |
printfn "%c" (char n)</syntaxhighlight> |
||
{{Out}}<pre>65 |
{{Out}}<pre>65 |
||
A</pre> |
A</pre> |
||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
< |
<syntaxhighlight lang=factor>CHAR: katakana-letter-a . |
||
"ア" first . |
"ア" first . |
||
12450 1string print</ |
12450 1string print</syntaxhighlight> |
||
=={{header|FALSE}}== |
=={{header|FALSE}}== |
||
< |
<syntaxhighlight lang=false>'A." |
||
"65,</ |
"65,</syntaxhighlight> |
||
=={{header|Fantom}}== |
=={{header|Fantom}}== |
||
A character is represented in single quotes: the 'toInt' method returns the code for the character. The 'toChar' method converts an integer into its respective character. |
A character is represented in single quotes: the 'toInt' method returns the code for the character. The 'toChar' method converts an integer into its respective character. |
||
< |
<syntaxhighlight lang=fantom>fansh> 97.toChar |
||
a |
a |
||
fansh> 'a'.toInt |
fansh> 'a'.toInt |
||
97</ |
97</syntaxhighlight> |
||
=={{header|Forth}}== |
=={{header|Forth}}== |
||
As with C, characters are just integers on the stack which are treated as ASCII. |
As with C, characters are just integers on the stack which are treated as ASCII. |
||
< |
<syntaxhighlight lang=forth>char a |
||
dup . \ 97 |
dup . \ 97 |
||
emit \ a</ |
emit \ a</syntaxhighlight> |
||
=={{header|Fortran}}== |
=={{header|Fortran}}== |
||
Functions ACHAR and IACHAR specifically work with the ASCII character set, while the results of CHAR and ICHAR will depend on the default character set being used. |
Functions ACHAR and IACHAR specifically work with the ASCII character set, while the results of CHAR and ICHAR will depend on the default character set being used. |
||
< |
<syntaxhighlight lang=fortran>WRITE(*,*) ACHAR(97), IACHAR("a") |
||
WRITE(*,*) CHAR(97), ICHAR("a")</ |
WRITE(*,*) CHAR(97), ICHAR("a")</syntaxhighlight> |
||
=={{header|Free Pascal}}== |
=={{header|Free Pascal}}== |
||
Line 992: | Line 992: | ||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
< |
<syntaxhighlight lang=freebasic> |
||
' FreeBASIC v1.05.0 win64 |
' FreeBASIC v1.05.0 win64 |
||
Print "a - > "; Asc("a") |
Print "a - > "; Asc("a") |
||
Line 1,000: | Line 1,000: | ||
Sleep |
Sleep |
||
End |
End |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 1,010: | Line 1,010: | ||
=={{header|Frink}}== |
=={{header|Frink}}== |
||
The function <code>char[x]</code> in Frink returns the numerical Unicode codepoints for a string or character, or returns the Unicode string for an integer value or array of integer values. The <code>chars[x]</code> returns an array even if the string is a single character. These functions also correctly handle upper-plane Unicode characters as a single codepoint. |
The function <code>char[x]</code> in Frink returns the numerical Unicode codepoints for a string or character, or returns the Unicode string for an integer value or array of integer values. The <code>chars[x]</code> returns an array even if the string is a single character. These functions also correctly handle upper-plane Unicode characters as a single codepoint. |
||
< |
<syntaxhighlight lang=frink>println[char["a"]] // prints 97 |
||
println[chars["a"]] // prints [97] (an array) |
println[chars["a"]] // prints [97] (an array) |
||
println[char[97]] // prints a |
println[char[97]] // prints a |
||
println[char["Frink rules!"]] // prints [70, 114, 105, 110, 107, 32, 114, 117, 108, 101, 115, 33] |
println[char["Frink rules!"]] // prints [70, 114, 105, 110, 107, 32, 114, 117, 108, 101, 115, 33] |
||
println[[70, 114, 105, 110, 107, 32, 114, 117, 108, 101, 115, 33]] // prints "Frink rules!"</ |
println[[70, 114, 105, 110, 107, 32, 114, 117, 108, 101, 115, 33]] // prints "Frink rules!"</syntaxhighlight> |
||
=={{header|Gambas}}== |
=={{header|Gambas}}== |
||
< |
<syntaxhighlight lang=gambas>Public Sub Form_Open() |
||
Dim sChar As String |
Dim sChar As String |
||
Line 1,026: | Line 1,026: | ||
Print "ASCII code " & sChar & " represents " & Chr(Val(sChar)) |
Print "ASCII code " & sChar & " represents " & Chr(Val(sChar)) |
||
End</ |
End</syntaxhighlight> |
||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 1,034: | Line 1,034: | ||
=={{header|GAP}}== |
=={{header|GAP}}== |
||
< |
<syntaxhighlight lang=gap># Code must be in 0 .. 255. |
||
CharInt(65); |
CharInt(65); |
||
# 'A' |
# 'A' |
||
IntChar('Z'); |
IntChar('Z'); |
||
# 90</ |
# 90</syntaxhighlight> |
||
=={{header|Go}}== |
=={{header|Go}}== |
||
In Go, a character literal ''is'' simply an integer constant of the character code: |
In Go, a character literal ''is'' simply an integer constant of the character code: |
||
< |
<syntaxhighlight lang=go>fmt.Println('a') // prints "97" |
||
fmt.Println('π') // prints "960"</ |
fmt.Println('π') // prints "960"</syntaxhighlight> |
||
< |
<syntaxhighlight lang=go>package main |
||
import ( |
import ( |
||
Line 1,055: | Line 1,055: | ||
// Given a code, print out the corresponding character. |
// Given a code, print out the corresponding character. |
||
fmt.Printf("%c\n", 65) // prt A |
fmt.Printf("%c\n", 65) // prt A |
||
}</ |
}</syntaxhighlight> |
||
Literal constants in Go are not typed (named constants can be). |
Literal constants in Go are not typed (named constants can be). |
||
The variable and constant types most commonly used for character data are <code>byte</code>, <code>rune</code>, and <code>string</code>. |
The variable and constant types most commonly used for character data are <code>byte</code>, <code>rune</code>, and <code>string</code>. |
||
This example program shows character codes (as literals) stored in typed variables, and printed out with default formatting. Note that since byte and rune are integer types, the default formatting is a printable base 10 number. String is not numeric, and a little extra work must be done to print the character codes. |
This example program shows character codes (as literals) stored in typed variables, and printed out with default formatting. Note that since byte and rune are integer types, the default formatting is a printable base 10 number. String is not numeric, and a little extra work must be done to print the character codes. |
||
< |
<syntaxhighlight lang=go>package main |
||
import "fmt" |
import "fmt" |
||
Line 1,079: | Line 1,079: | ||
// We can also print the bytes of a string without an explicit loop |
// We can also print the bytes of a string without an explicit loop |
||
fmt.Printf("\n string bytes: % #x\n", s) |
fmt.Printf("\n string bytes: % #x\n", s) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,088: | Line 1,088: | ||
</pre> |
</pre> |
||
For the second part of the task, printing the character of a given code, the <code>%c</code> verb of <code>fmt.Printf</code> will do this directly from integer values, emitting the UTF-8 encoding of the code, (which will typically print the character depending on your hardware and operating system configuration). |
For the second part of the task, printing the character of a given code, the <code>%c</code> verb of <code>fmt.Printf</code> will do this directly from integer values, emitting the UTF-8 encoding of the code, (which will typically print the character depending on your hardware and operating system configuration). |
||
< |
<syntaxhighlight lang=go>b := byte(97) |
||
r := rune(960) |
r := rune(960) |
||
fmt.Printf("%c %c\n%c %c\n", 97, 960, b, r)</ |
fmt.Printf("%c %c\n%c %c\n", 97, 960, b, r)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,099: | Line 1,099: | ||
Examples showing strings constructed from integer constants and then printed: |
Examples showing strings constructed from integer constants and then printed: |
||
< |
<syntaxhighlight lang=go>fmt.Println(string(97)) // prints "a" |
||
fmt.Println(string(960)) // prints "π" |
fmt.Println(string(960)) // prints "π" |
||
fmt.Println(string([]rune{97, 960})) // prints "aπ"</ |
fmt.Println(string([]rune{97, 960})) // prints "aπ"</syntaxhighlight> |
||
=={{header|Golfscript}}== |
=={{header|Golfscript}}== |
||
To convert a number to a string, we use the array to string coercion. |
To convert a number to a string, we use the array to string coercion. |
||
<lang |
<syntaxhighlight lang=golfscript>97[]+''+p</syntaxhighlight> |
||
To convert a string to a number, we have a many options, of which the simplest and shortest are: |
To convert a string to a number, we have a many options, of which the simplest and shortest are: |
||
< |
<syntaxhighlight lang=golfscript>'a')\;p |
||
'a'(\;p |
'a'(\;p |
||
'a'0=p |
'a'0=p |
||
'a'{}/p</ |
'a'{}/p</syntaxhighlight> |
||
=={{header|Groovy}}== |
=={{header|Groovy}}== |
||
Groovy does not have a character literal at all, so one-character strings have to be ''coerced'' to '''char'''. Groovy '''printf''' (like Java, but unlike C) is ''not type-agnostic'', so the cast or coercion from '''char''' to '''int''' is also required. The reverse direction is considerably simpler. |
Groovy does not have a character literal at all, so one-character strings have to be ''coerced'' to '''char'''. Groovy '''printf''' (like Java, but unlike C) is ''not type-agnostic'', so the cast or coercion from '''char''' to '''int''' is also required. The reverse direction is considerably simpler. |
||
< |
<syntaxhighlight lang=groovy>printf ("%d\n", ('a' as char) as int) |
||
printf ("%c\n", 97)</ |
printf ("%c\n", 97)</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>97 |
<pre>97 |
||
Line 1,121: | Line 1,121: | ||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
< |
<syntaxhighlight lang=haskell>import Data.Char |
||
main = do |
main = do |
||
Line 1,127: | Line 1,127: | ||
print (chr 97) -- prints "'a'" |
print (chr 97) -- prints "'a'" |
||
print (ord 'π') -- prints "960" |
print (ord 'π') -- prints "960" |
||
print (chr 960) -- prints "'\960'"</ |
print (chr 960) -- prints "'\960'"</syntaxhighlight> |
||
=={{header|HicEst}}== |
=={{header|HicEst}}== |
||
< |
<syntaxhighlight lang=hicest>WRITE(Messagebox) ICHAR('a'), CHAR(97)</syntaxhighlight> |
||
=={{header|HolyC}}== |
=={{header|HolyC}}== |
||
< |
<syntaxhighlight lang=holyc>Print("%d\n", 'a'); /* prints "97" */ |
||
Print("%c\n", 97); /* prints "a" */</ |
Print("%c\n", 97); /* prints "a" */</syntaxhighlight> |
||
=={{header|Hoon}}== |
=={{header|Hoon}}== |
||
< |
<syntaxhighlight lang=hoon>|% |
||
++ enc |
++ enc |
||
|= char=@t `@ud`char |
|= char=@t `@ud`char |
||
++ dec |
++ dec |
||
|= code=@ud `@t`code |
|= code=@ud `@t`code |
||
--</ |
--</syntaxhighlight> |
||
=={{header|i}}== |
=={{header|i}}== |
||
< |
<syntaxhighlight lang=i>software { |
||
print(number('a')) |
print(number('a')) |
||
print(text([97])) |
print(text([97])) |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Icon}} and {{header|Unicon}}== |
=={{header|Icon}} and {{header|Unicon}}== |
||
< |
<syntaxhighlight lang=Icon>procedure main(arglist) |
||
if *arglist > 0 then L := arglist else L := [97, "a"] |
if *arglist > 0 then L := arglist else L := [97, "a"] |
||
every x := !L do |
every x := !L do |
||
write(x, " ==> ", char(integer(x)) | ord(x) ) # char produces a character, ord produces a number |
write(x, " ==> ", char(integer(x)) | ord(x) ) # char produces a character, ord produces a number |
||
end</ |
end</syntaxhighlight> |
||
Icon and Unicon do not currently support double byte character sets. |
Icon and Unicon do not currently support double byte character sets. |
||
{{Out}}<pre>97 ==> a |
{{Out}}<pre>97 ==> a |
||
Line 1,163: | Line 1,163: | ||
=={{header|Io}}== |
=={{header|Io}}== |
||
Here character is a sequence (string) of length one. |
Here character is a sequence (string) of length one. |
||
< |
<syntaxhighlight lang=Io>"a" at(0) println // --> 97 |
||
97 asCharacter println // --> a |
97 asCharacter println // --> a |
||
"π" at(0) println // --> 960 |
"π" at(0) println // --> 960 |
||
960 asCharacter println // --> π</ |
960 asCharacter println // --> π</syntaxhighlight> |
||
=={{header|J}}== |
=={{header|J}}== |
||
< |
<syntaxhighlight lang=j> 4 u: 97 98 99 9786 |
||
abc☺ |
abc☺ |
||
3 u: 7 u: 'abc☺' |
3 u: 7 u: 'abc☺' |
||
97 98 99 9786</ |
97 98 99 9786</syntaxhighlight> |
||
<code>7 u:</code> converts from utf-8, <code>3 u:</code> by itself would give us: |
<code>7 u:</code> converts from utf-8, <code>3 u:</code> by itself would give us: |
||
< |
<syntaxhighlight lang=j> 3 u: 'abc☺' |
||
97 98 99 226 152 186</ |
97 98 99 226 152 186</syntaxhighlight> |
||
Also, if we limit ourselves to ascii, we have other ways of accomplishing the same thing. <code>a.</code> is a list of the 8 bit character codes and we can index from it, or search it (though that's mostly a notational convenience, since the underlying type already gives us all we need to know). |
Also, if we limit ourselves to ascii, we have other ways of accomplishing the same thing. <code>a.</code> is a list of the 8 bit character codes and we can index from it, or search it (though that's mostly a notational convenience, since the underlying type already gives us all we need to know). |
||
< |
<syntaxhighlight lang=j> 97 98 99{a. |
||
abc |
abc |
||
a.i.'abc' |
a.i.'abc' |
||
97 98 99</ |
97 98 99</syntaxhighlight> |
||
=={{header|Java}}== |
=={{header|Java}}== |
||
Line 1,192: | Line 1,192: | ||
In this case, the <tt>println</tt> method is overloaded to handle integer (outputs the decimal representation) and character (outputs just the character) types differently, so we need to cast it in both cases. |
In this case, the <tt>println</tt> method is overloaded to handle integer (outputs the decimal representation) and character (outputs just the character) types differently, so we need to cast it in both cases. |
||
< |
<syntaxhighlight lang=java>public class Foo { |
||
public static void main(String[] args) { |
public static void main(String[] args) { |
||
System.out.println((int)'a'); // prints "97" |
System.out.println((int)'a'); // prints "97" |
||
System.out.println((char)97); // prints "a" |
System.out.println((char)97); // prints "a" |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
Java characters support Unicode: |
Java characters support Unicode: |
||
< |
<syntaxhighlight lang=java>public class Bar { |
||
public static void main(String[] args) { |
public static void main(String[] args) { |
||
System.out.println((int)'π'); // prints "960" |
System.out.println((int)'π'); // prints "960" |
||
System.out.println((char)960); // prints "π" |
System.out.println((char)960); // prints "π" |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|JavaScript}}== |
=={{header|JavaScript}}== |
||
Here character is just a string of length 1 |
Here character is just a string of length 1 |
||
< |
<syntaxhighlight lang=javascript>console.log('a'.charCodeAt(0)); // prints "97" |
||
console.log(String.fromCharCode(97)); // prints "a"</ |
console.log(String.fromCharCode(97)); // prints "a"</syntaxhighlight> |
||
ES6 brings '''String.codePointAt()''' and '''String.fromCodePoint()''', which provide access to 4-byte unicode characters, |
ES6 brings '''String.codePointAt()''' and '''String.fromCodePoint()''', which provide access to 4-byte unicode characters, |
||
in addition to the usual 2-byte unicode characters. |
in addition to the usual 2-byte unicode characters. |
||
< |
<syntaxhighlight lang=JavaScript>['字'.codePointAt(0), '🐘'.codePointAt(0)]</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<lang |
<syntaxhighlight lang=JavaScript>[23383, 128024]</syntaxhighlight> |
||
and |
and |
||
< |
<syntaxhighlight lang=JavaScript>[23383, 128024].map(function (x) { |
||
return String.fromCodePoint(x); |
return String.fromCodePoint(x); |
||
})</ |
})</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<lang |
<syntaxhighlight lang=JavaScript>["字", "🐘"]</syntaxhighlight> |
||
=={{header|Joy}}== |
=={{header|Joy}}== |
||
< |
<syntaxhighlight lang=joy>'a ord. |
||
97 chr.</ |
97 chr.</syntaxhighlight> |
||
=={{header|jq}}== |
=={{header|jq}}== |
||
jq data strings are JSON strings, which can be "explode"d into an array of integers, each representing a Unicode codepoint. The inverse of the <tt>explode</tt> filter is <tt>implode</tt>. <tt>explode</tt> can of course be used for single-character strings, and so for example: |
jq data strings are JSON strings, which can be "explode"d into an array of integers, each representing a Unicode codepoint. The inverse of the <tt>explode</tt> filter is <tt>implode</tt>. <tt>explode</tt> can of course be used for single-character strings, and so for example: |
||
< |
<syntaxhighlight lang=jq>"a" | explode # => [ 97 ] |
||
[97] | implode # => "a"</ |
[97] | implode # => "a"</syntaxhighlight> |
||
Here is a filter which can be used to convert an integer to the corresponding |
Here is a filter which can be used to convert an integer to the corresponding |
||
character:< |
character:<syntaxhighlight lang=jq>def chr: [.] | implode; |
||
</syntaxhighlight> |
|||
</lang> |
|||
Example: |
Example: |
||
1024 | chr # => "Ѐ" |
1024 | chr # => "Ѐ" |
||
Line 1,247: | Line 1,247: | ||
Julia character constants (of type <code>Char</code>) are treated as an integer type representing the Unicode codepoint of the character, and can easily be converted to and from other integer types. |
Julia character constants (of type <code>Char</code>) are treated as an integer type representing the Unicode codepoint of the character, and can easily be converted to and from other integer types. |
||
< |
<syntaxhighlight lang=julia>println(Int('a')) |
||
println(Char(97))</ |
println(Char(97))</syntaxhighlight> |
||
{{out}}<pre>97 |
{{out}}<pre>97 |
||
Line 1,254: | Line 1,254: | ||
=={{header|K}}== |
=={{header|K}}== |
||
< |
<syntaxhighlight lang=K> _ic "abcABC" |
||
97 98 99 65 66 67 |
97 98 99 65 66 67 |
||
_ci 97 98 99 65 66 67 |
_ci 97 98 99 65 66 67 |
||
"abcABC"</ |
"abcABC"</syntaxhighlight> |
||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
< |
<syntaxhighlight lang=scala>fun main(args: Array<String>) { |
||
var c = 'a' |
var c = 'a' |
||
var i = c.toInt() |
var i = c.toInt() |
||
Line 1,268: | Line 1,268: | ||
c = i.toChar() |
c = i.toChar() |
||
println("$i <-> $c") |
println("$i <-> $c") |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,281: | Line 1,281: | ||
=={{header|Lang5}}== |
=={{header|Lang5}}== |
||
< |
<syntaxhighlight lang=lang5>: CHAR "!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[" comb |
||
'\\ comb -1 remove append "]^_`abcdefghijklmnopqrstuvwxyz{|}~" comb append ; |
'\\ comb -1 remove append "]^_`abcdefghijklmnopqrstuvwxyz{|}~" comb append ; |
||
: CODE 95 iota 33 + ; : comb "" split ; |
: CODE 95 iota 33 + ; : comb "" split ; |
||
Line 1,289: | Line 1,289: | ||
'a ord . # 97 |
'a ord . # 97 |
||
97 chr . # a</ |
97 chr . # a</syntaxhighlight> |
||
=={{header|langur}}== |
=={{header|langur}}== |
||
Line 1,296: | Line 1,296: | ||
The s2cp() and cp2s() functions convert between code point integers and strings. Also, string indexing is by code point. |
The s2cp() and cp2s() functions convert between code point integers and strings. Also, string indexing is by code point. |
||
< |
<syntaxhighlight lang=langur>val .a1 = 'a' |
||
val .a2 = 97 |
val .a2 = 97 |
||
val .a3 = "a"[1] |
val .a3 = "a"[1] |
||
Line 1,306: | Line 1,306: | ||
writeln .a3 == .a4 |
writeln .a3 == .a4 |
||
writeln "numbers: ", join ", ", [.a1, .a2, .a3, .a4, .a5] |
writeln "numbers: ", join ", ", [.a1, .a2, .a3, .a4, .a5] |
||
writeln "letters: ", join ", ", [cp2s(.a1), cp2s(.a2), cp2s(.a3), cp2s(.a4), cp2s(.a5)]</ |
writeln "letters: ", join ", ", [cp2s(.a1), cp2s(.a2), cp2s(.a3), cp2s(.a4), cp2s(.a5)]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,317: | Line 1,317: | ||
=={{header|Lasso}}== |
=={{header|Lasso}}== |
||
< |
<syntaxhighlight lang=Lasso>'a'->integer |
||
'A'->integer |
'A'->integer |
||
97->bytes |
97->bytes |
||
65->bytes</ |
65->bytes</syntaxhighlight> |
||
{{out}}<pre>97 |
{{out}}<pre>97 |
||
65 |
65 |
||
Line 1,328: | Line 1,328: | ||
=={{header|LFE}}== |
=={{header|LFE}}== |
||
In LFE/Erlang, lists and strings are the same, only the representation changes. For example: |
In LFE/Erlang, lists and strings are the same, only the representation changes. For example: |
||
< |
<syntaxhighlight lang=lisp>> (list 68 111 110 39 116 32 80 97 110 105 99 46) |
||
"Don't Panic."</ |
"Don't Panic."</syntaxhighlight> |
||
As for this exercise, here's how you could print out the ASCII code for a letter, and a letter from the ASCII code: |
As for this exercise, here's how you could print out the ASCII code for a letter, and a letter from the ASCII code: |
||
< |
<syntaxhighlight lang=lisp>> (: io format '"~w~n" '"a") |
||
97 |
97 |
||
ok |
ok |
||
> (: io format '"~p~n" (list '(97))) |
> (: io format '"~p~n" (list '(97))) |
||
"a" |
"a" |
||
ok</ |
ok</syntaxhighlight> |
||
=={{header|Liberty BASIC}}== |
=={{header|Liberty BASIC}}== |
||
< |
<syntaxhighlight lang=lb>charCode = 97 |
||
char$ = "a" |
char$ = "a" |
||
print chr$(charCode) 'prints a |
print chr$(charCode) 'prints a |
||
print asc(char$) 'prints 97</ |
print asc(char$) 'prints 97</syntaxhighlight> |
||
=={{header|LIL}}== |
=={{header|LIL}}== |
||
LIL does not handle NUL bytes in character strings, char 0 returns an empty string. |
LIL does not handle NUL bytes in character strings, char 0 returns an empty string. |
||
< |
<syntaxhighlight lang=tcl>print [char 97] |
||
print [codeat "a" 0]</ |
print [codeat "a" 0]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,355: | Line 1,355: | ||
=={{header|Lingo}}== |
=={{header|Lingo}}== |
||
< |
<syntaxhighlight lang=lingo>-- returns Unicode code point (=ASCII code for ASCII characters) for character |
||
put chartonum("a") |
put chartonum("a") |
||
-- 97 |
-- 97 |
||
Line 1,361: | Line 1,361: | ||
-- returns character for Unicode code point (=ASCII code for ASCII characters) |
-- returns character for Unicode code point (=ASCII code for ASCII characters) |
||
put numtochar(934) |
put numtochar(934) |
||
-- Φ</ |
-- Φ</syntaxhighlight> |
||
=={{header|Little}}== |
=={{header|Little}}== |
||
< |
<syntaxhighlight lang=C>puts("Unicode value of ñ is ${scan("ñ", "%c")}"); |
||
printf("The code 241 in Unicode is the letter: %c.\n", 241); |
printf("The code 241 in Unicode is the letter: %c.\n", 241); |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|LiveCode}}== |
=={{header|LiveCode}}== |
||
< |
<syntaxhighlight lang=LiveCode>Since 7.0.x works with unicode |
||
put charToNum("") && numToChar(240)</ |
put charToNum("") && numToChar(240)</syntaxhighlight> |
||
=={{header|Logo}}== |
=={{header|Logo}}== |
||
Logo characters are words of length 1. |
Logo characters are words of length 1. |
||
< |
<syntaxhighlight lang=logo>print ascii "a ; 97 |
||
print char 97 ; a</ |
print char 97 ; a</syntaxhighlight> |
||
=={{header|Logtalk}}== |
=={{header|Logtalk}}== |
||
< |
<syntaxhighlight lang=logtalk>|?- char_code(Char, 97), write(Char). |
||
a |
a |
||
Char = a |
Char = a |
||
yes</ |
yes</syntaxhighlight> |
||
< |
<syntaxhighlight lang=logtalk>|?- char_code(a, Code), write(Code). |
||
97 |
97 |
||
Code = 97 |
Code = 97 |
||
yes</ |
yes</syntaxhighlight> |
||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
< |
<syntaxhighlight lang=lua>print(string.byte("a")) -- prints "97" |
||
print(string.char(97)) -- prints "a"</ |
print(string.char(97)) -- prints "a"</syntaxhighlight> |
||
=={{header|M2000 Interpreter}}== |
=={{header|M2000 Interpreter}}== |
||
< |
<syntaxhighlight lang=M2000 Interpreter> |
||
\\ ANSI |
\\ ANSI |
||
Print Asc("a") |
Print Asc("a") |
||
Line 1,415: | Line 1,415: | ||
Print Codes("abcd") |
Print Codes("abcd") |
||
\\ 97 98 99 100 |
\\ 97 98 99 100 |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Maple}}== |
=={{header|Maple}}== |
||
There are two ways to do this in Maple. First, there are procedures in StringTools for this purpose. |
There are two ways to do this in Maple. First, there are procedures in StringTools for this purpose. |
||
< |
<syntaxhighlight lang=Maple>> use StringTools in Ord( "A" ); Char( 65 ) end; |
||
65 |
65 |
||
"A" |
"A" |
||
</syntaxhighlight> |
|||
</lang> |
|||
Second, the procedure convert handles conversions to and from byte values. |
Second, the procedure convert handles conversions to and from byte values. |
||
< |
<syntaxhighlight lang=Maple>> convert( "A", bytes ); |
||
[65] |
[65] |
||
> convert( [65], bytes ); |
> convert( [65], bytes ); |
||
"A" |
"A" |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Mathematica}} / {{header|Wolfram Language}}== |
=={{header|Mathematica}} / {{header|Wolfram Language}}== |
||
Use the FromCharacterCode and ToCharacterCode functions: |
Use the FromCharacterCode and ToCharacterCode functions: |
||
< |
<syntaxhighlight lang=Mathematica>ToCharacterCode["abcd"] |
||
FromCharacterCode[{97}]</ |
FromCharacterCode[{97}]</syntaxhighlight> |
||
{{Out}}<pre>{97, 98, 99, 100} |
{{Out}}<pre>{97, 98, 99, 100} |
||
Line 1,443: | Line 1,443: | ||
There are two built-in function that perform these tasks. |
There are two built-in function that perform these tasks. |
||
To convert from a number to a character use: |
To convert from a number to a character use: |
||
< |
<syntaxhighlight lang=MATLAB>character = char(asciiNumber)</syntaxhighlight> |
||
To convert from a character to its corresponding ascii character use: |
To convert from a character to its corresponding ascii character use: |
||
< |
<syntaxhighlight lang=MATLAB>asciiNumber = double(character)</syntaxhighlight> |
||
or if you need this number as an integer not a double use: |
or if you need this number as an integer not a double use: |
||
< |
<syntaxhighlight lang=MATLAB>asciiNumber = uint16(character) |
||
asciiNumber = uint32(character) |
asciiNumber = uint32(character) |
||
asciiNumber = uint64(character)</ |
asciiNumber = uint64(character)</syntaxhighlight> |
||
Sample Usage: |
Sample Usage: |
||
< |
<syntaxhighlight lang=MATLAB>>> char(87) |
||
ans = |
ans = |
||
Line 1,470: | Line 1,470: | ||
ans = |
ans = |
||
87</ |
87</syntaxhighlight> |
||
=={{header|Maxima}}== |
=={{header|Maxima}}== |
||
< |
<syntaxhighlight lang=maxima>ascii(65); |
||
"A" |
"A" |
||
cint("A"); |
cint("A"); |
||
65</ |
65</syntaxhighlight> |
||
=={{header|Metafont}}== |
=={{header|Metafont}}== |
||
Metafont handles only ''ASCII'' (even though codes beyond 127 can be given and used as real ASCII codes) |
Metafont handles only ''ASCII'' (even though codes beyond 127 can be given and used as real ASCII codes) |
||
< |
<syntaxhighlight lang=metafont>message "enter a letter: "; |
||
string a; |
string a; |
||
a := readstring; |
a := readstring; |
||
Line 1,494: | Line 1,494: | ||
message char10; % (this add a newline...) |
message char10; % (this add a newline...) |
||
message char hex"c3" & char hex"a8"; % since C3 A8 is the UTF-8 encoding for "è" |
message char hex"c3" & char hex"a8"; % since C3 A8 is the UTF-8 encoding for "è" |
||
end</ |
end</syntaxhighlight> |
||
=={{header|Microsoft Small Basic}}== |
=={{header|Microsoft Small Basic}}== |
||
< |
<syntaxhighlight lang=vb>TextWindow.WriteLine("The ascii code for 'A' is: " + Text.GetCharacterCode("A") + ".") |
||
TextWindow.WriteLine("The character for '65' is: " + Text.GetCharacter(65) + ".")</ |
TextWindow.WriteLine("The character for '65' is: " + Text.GetCharacter(65) + ".")</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
< |
<syntaxhighlight lang=basic>The ascii code for 'A' is: 65. |
||
The character for '65' is: A. |
The character for '65' is: A. |
||
Press any key to continue...</ |
Press any key to continue...</syntaxhighlight> |
||
=={{header|Modula-2}}== |
=={{header|Modula-2}}== |
||
< |
<syntaxhighlight lang=modula2>MODULE asc; |
||
IMPORT InOut; |
IMPORT InOut; |
||
Line 1,523: | Line 1,523: | ||
InOut.Write (CHR (ascii)); |
InOut.Write (CHR (ascii)); |
||
InOut.WriteLn |
InOut.WriteLn |
||
END asc.</ |
END asc.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
< |
<syntaxhighlight lang=Modula-2>jan@Beryllium:~/modula/rosetta$ ./asc |
||
a 97 1</ |
a 97 1</syntaxhighlight> |
||
=={{header|Modula-3}}== |
=={{header|Modula-3}}== |
||
The built in functions <code>ORD</code> and <code>VAL</code> work on characters, among other things. |
The built in functions <code>ORD</code> and <code>VAL</code> work on characters, among other things. |
||
< |
<syntaxhighlight lang=modula3>ORD('a') (* Returns 97 *) |
||
VAL(97, CHAR); (* Returns 'a' *)</ |
VAL(97, CHAR); (* Returns 'a' *)</syntaxhighlight> |
||
=={{header|MUMPS}}== |
=={{header|MUMPS}}== |
||
< |
<syntaxhighlight lang=MUMPS>WRITE $ASCII("M") |
||
WRITE $CHAR(77)</ |
WRITE $CHAR(77)</syntaxhighlight> |
||
=={{header|Nanoquery}}== |
=={{header|Nanoquery}}== |
||
< |
<syntaxhighlight lang=Nanoquery>println ord("a") |
||
println chr(97) |
println chr(97) |
||
println ord("π") |
println ord("π") |
||
println chr(960)</ |
println chr(960)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>97 |
<pre>97 |
||
Line 1,552: | Line 1,552: | ||
Neko treats strings as an array of bytes |
Neko treats strings as an array of bytes |
||
< |
<syntaxhighlight lang=neko>// An 'a' and a 'b' |
||
var s = "a"; |
var s = "a"; |
||
var c = 98; |
var c = 98; |
||
Line 1,560: | Line 1,560: | ||
$sset(h, 0, c); |
$sset(h, 0, c); |
||
$print("Character code ", c, ": ", h, "\n");</ |
$print("Character code ", c, ": ", h, "\n");</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,568: | Line 1,568: | ||
Neko also has standard primitives for handling the byte array as UTF-8 |
Neko also has standard primitives for handling the byte array as UTF-8 |
||
< |
<syntaxhighlight lang=neko>// While Neko also includes some UTF-8 operations, |
||
// native strings are just arrays of bytes |
// native strings are just arrays of bytes |
||
var us = "¥·£·€·$·¢·₡·₢·₣·₤·₥·₦·₧·₨·₩·₪·₫·₭·₮·₯·₹"; |
var us = "¥·£·€·$·¢·₡·₢·₣·₤·₥·₦·₧·₨·₩·₪·₫·₭·₮·₯·₹"; |
||
Line 1,589: | Line 1,589: | ||
uc = 8356; |
uc = 8356; |
||
utfAdd(buf, uc); |
utfAdd(buf, uc); |
||
$print("UTF-8 code ", uc, ": ", utfContent(buf), "\n");</ |
$print("UTF-8 code ", uc, ": ", utfContent(buf), "\n");</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,597: | Line 1,597: | ||
=={{header|NESL}}== |
=={{header|NESL}}== |
||
In NESL, character literals are prefixed with a backtick. The functions <tt>char_code</tt> and <tt>code_char</tt> convert between characters and integer character codes. |
In NESL, character literals are prefixed with a backtick. The functions <tt>char_code</tt> and <tt>code_char</tt> convert between characters and integer character codes. |
||
< |
<syntaxhighlight lang=nesl>char_code(`a); |
||
it = 97 : int</ |
it = 97 : int</syntaxhighlight> |
||
< |
<syntaxhighlight lang=nesl>code_char(97); |
||
it = `a : char</ |
it = `a : char</syntaxhighlight> |
||
=={{header|NetRexx}}== |
=={{header|NetRexx}}== |
||
NetRexx provides built-in functions to convert between character and decimal/hexadecimal. |
NetRexx provides built-in functions to convert between character and decimal/hexadecimal. |
||
< |
<syntaxhighlight lang=NetRexx>/* NetRexx */ |
||
options replace format comments java crossref symbols nobinary |
options replace format comments java crossref symbols nobinary |
||
Line 1,628: | Line 1,628: | ||
say ci.right(3)"| '"cc"'" cd.right(6) cx.right(4, 0) "'"dc"' '"xc"'" |
say ci.right(3)"| '"cc"'" cd.right(6) cx.right(4, 0) "'"dc"' '"xc"'" |
||
end ci |
end ci |
||
return</ |
return</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre style="height:20ex; overflow:scroll">' abcde$¢£¤¥₠₡₢₣₤₥₦₧₨₩₪₫€₭₮₯₰₱₲₳₴₵' |
<pre style="height:20ex; overflow:scroll">' abcde$¢£¤¥₠₡₢₣₤₥₦₧₨₩₪₫€₭₮₯₰₱₲₳₴₵' |
||
Line 1,668: | Line 1,668: | ||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
< |
<syntaxhighlight lang=nim>echo ord('a') # echoes 97 |
||
echo chr(97) # echoes a |
echo chr(97) # echoes a |
||
Line 1,674: | Line 1,674: | ||
echo int("π".runeAt(0)) # echoes 960 |
echo int("π".runeAt(0)) # echoes 960 |
||
echo Rune(960) # echoes π</ |
echo Rune(960) # echoes π</syntaxhighlight> |
||
=={{header|NS-HUBASIC}}== |
=={{header|NS-HUBASIC}}== |
||
NS-HUBASIC uses a non-ASCII character set that doesn't include letters in lowercase. |
NS-HUBASIC uses a non-ASCII character set that doesn't include letters in lowercase. |
||
< |
<syntaxhighlight lang=NS-HUBASIC>10 PRINT CODE "A" |
||
20 PRINT CHR$(38)</ |
20 PRINT CHR$(38)</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre> 0A |
<pre> 0A |
||
Line 1,685: | Line 1,685: | ||
=={{header|Oberon-2}}== |
=={{header|Oberon-2}}== |
||
< |
<syntaxhighlight lang=oberon2>MODULE Ascii; |
||
IMPORT Out; |
IMPORT Out; |
||
VAR |
VAR |
||
Line 1,695: | Line 1,695: | ||
Out.Int(d,3);Out.Ln; |
Out.Int(d,3);Out.Ln; |
||
Out.Char(c);Out.Ln |
Out.Char(c);Out.Ln |
||
END Ascii.</ |
END Ascii.</syntaxhighlight> |
||
{{Out}}<pre> |
{{Out}}<pre> |
||
97 |
97 |
||
Line 1,701: | Line 1,701: | ||
=={{header|Objeck}}== |
=={{header|Objeck}}== |
||
< |
<syntaxhighlight lang=objeck>'a'->As(Int)->PrintLine(); |
||
97->As(Char)->PrintLine();</ |
97->As(Char)->PrintLine();</syntaxhighlight> |
||
=={{header|Object Pascal}}== |
=={{header|Object Pascal}}== |
||
Line 1,708: | Line 1,708: | ||
=={{header|OCaml}}== |
=={{header|OCaml}}== |
||
< |
<syntaxhighlight lang=ocaml>Printf.printf "%d\n" (int_of_char 'a'); (* prints "97" *) |
||
Printf.printf "%c\n" (char_of_int 97); (* prints "a" *)</ |
Printf.printf "%c\n" (char_of_int 97); (* prints "a" *)</syntaxhighlight> |
||
The following are aliases for the above functions: |
The following are aliases for the above functions: |
||
< |
<syntaxhighlight lang=ocaml># Char.code ;; |
||
- : char -> int = <fun> |
- : char -> int = <fun> |
||
# Char.chr;; |
# Char.chr;; |
||
- : int -> char = <fun></ |
- : int -> char = <fun></syntaxhighlight> |
||
=={{header|Oforth}}== |
=={{header|Oforth}}== |
||
Line 1,721: | Line 1,721: | ||
Oforth has not type or class for characters. A character is an integer which value is its unicode code. |
Oforth has not type or class for characters. A character is an integer which value is its unicode code. |
||
<lang |
<syntaxhighlight lang=Oforth>'a' println</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,729: | Line 1,729: | ||
=={{header|OpenEdge/Progress}}== |
=={{header|OpenEdge/Progress}}== |
||
< |
<syntaxhighlight lang=Progress (Openedge ABL)>MESSAGE |
||
CHR(97) SKIP |
CHR(97) SKIP |
||
ASC("a") |
ASC("a") |
||
VIEW-AS ALERT-BOX.</ |
VIEW-AS ALERT-BOX.</syntaxhighlight> |
||
=={{header|Oz}}== |
=={{header|Oz}}== |
||
Characters in Oz are the same as integers in the range 0-255 (ISO 8859-1 encoding). To print a number as a character, we need to use it as a string (i.e. a list of integers from 0 to 255): |
Characters in Oz are the same as integers in the range 0-255 (ISO 8859-1 encoding). To print a number as a character, we need to use it as a string (i.e. a list of integers from 0 to 255): |
||
< |
<syntaxhighlight lang=oz>{System.show &a} %% prints "97" |
||
{System.showInfo [97]} %% prints "a"</ |
{System.showInfo [97]} %% prints "a"</syntaxhighlight> |
||
=={{header|PARI/GP}}== |
=={{header|PARI/GP}}== |
||
< |
<syntaxhighlight lang=parigp>print(Vecsmall("a")[1]); |
||
print(Strchr([72, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100, 33]))</ |
print(Strchr([72, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100, 33]))</syntaxhighlight> |
||
=={{header|Pascal}}== |
=={{header|Pascal}}== |
||
< |
<syntaxhighlight lang=pascal>writeln(ord('a')); |
||
writeln(chr(97));</ |
writeln(chr(97));</syntaxhighlight> |
||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
===Narrow=== |
===Narrow=== |
||
The code is straightforward when characters are all narrow (single byte). |
The code is straightforward when characters are all narrow (single byte). |
||
< |
<syntaxhighlight lang=perl>use strict; |
||
use warnings; |
use warnings; |
||
use utf8; |
use utf8; |
||
Line 1,768: | Line 1,768: | ||
} |
} |
||
print "\n"; |
print "\n"; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> Character: A |
<pre> Character: A |
||
Line 1,800: | Line 1,800: | ||
===Wide=== |
===Wide=== |
||
Have to work a little harder to handle wide (multi-byte) characters. |
Have to work a little harder to handle wide (multi-byte) characters. |
||
< |
<syntaxhighlight lang=perl>use strict; |
||
use warnings; |
use warnings; |
||
use feature 'say'; |
use feature 'say'; |
||
Line 1,818: | Line 1,818: | ||
'UTF-8', join('', map { sprintf "%x ", ord } (utf8::encode($c), split //, $c)), |
'UTF-8', join('', map { sprintf "%x ", ord } (utf8::encode($c), split //, $c)), |
||
'Round trip', join('', map { chr } @ordinals); |
'Round trip', join('', map { chr } @ordinals); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> Character: Δ̂ |
<pre> Character: Δ̂ |
||
Line 1,848: | Line 1,848: | ||
Characters and their ascii codes are one and the same. (See also printf, %d / %s / %c.) |
Characters and their ascii codes are one and the same. (See also printf, %d / %s / %c.) |
||
<!--< |
<!--<syntaxhighlight lang=Phix>--> |
||
<span style="color: #0000FF;">?<span style="color: #008000;">'A'</span> |
<span style="color: #0000FF;">?<span style="color: #008000;">'A'</span> |
||
<span style="color: #7060A8;">puts<span style="color: #0000FF;">(<span style="color: #000000;">1<span style="color: #0000FF;">,<span style="color: #000000;">65<span style="color: #0000FF;">) |
<span style="color: #7060A8;">puts<span style="color: #0000FF;">(<span style="color: #000000;">1<span style="color: #0000FF;">,<span style="color: #000000;">65<span style="color: #0000FF;">) |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{out}} |
||
Line 1,860: | Line 1,860: | ||
=={{header|Phixmonti}}== |
=={{header|Phixmonti}}== |
||
< |
<syntaxhighlight lang=Phixmonti>'a' print nl |
||
97 tochar print</ |
97 tochar print</syntaxhighlight> |
||
=={{header|PHP}}== |
=={{header|PHP}}== |
||
Here character is just a string of length 1 |
Here character is just a string of length 1 |
||
< |
<syntaxhighlight lang=php>echo ord('a'), "\n"; // prints "97" |
||
echo chr(97), "\n"; // prints "a"</ |
echo chr(97), "\n"; // prints "a"</syntaxhighlight> |
||
=={{header|Picat}}== |
=={{header|Picat}}== |
||
< |
<syntaxhighlight lang=Picat>main => |
||
println(chr(97)), |
println(chr(97)), |
||
println(ord('a')), |
println(ord('a')), |
||
println(ord(a)).</ |
println(ord(a)).</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,880: | Line 1,880: | ||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |
||
< |
<syntaxhighlight lang=PicoLisp>: (char "a") |
||
-> 97 |
-> 97 |
||
: (char "字") |
: (char "字") |
||
Line 1,889: | Line 1,889: | ||
-> ("文" "字") |
-> ("文" "字") |
||
: (mapcar char @) |
: (mapcar char @) |
||
-> (25991 23383)</ |
-> (25991 23383)</syntaxhighlight> |
||
=={{header|PL/I}}== |
=={{header|PL/I}}== |
||
< |
<syntaxhighlight lang=PL/I>declare 1 u union, |
||
2 c character (1), |
2 c character (1), |
||
2 i fixed binary (8) unsigned; |
2 i fixed binary (8) unsigned; |
||
c = 'a'; put skip list (i); /* prints 97 */ |
c = 'a'; put skip list (i); /* prints 97 */ |
||
i = 97; put skip list (c); /* prints 'a' */</ |
i = 97; put skip list (c); /* prints 'a' */</syntaxhighlight> |
||
=={{header|PowerShell}}== |
=={{header|PowerShell}}== |
||
Powershell does allow for character literals with [convert] |
Powershell does allow for character literals with [convert] |
||
< |
<syntaxhighlight lang=powershell>$char = [convert]::toChar(0x2f) #=> /</syntaxhighlight> |
||
PowerShell does not allow for character literals directly, so to get a character one first needs to convert a single-character string to a char: |
PowerShell does not allow for character literals directly, so to get a character one first needs to convert a single-character string to a char: |
||
< |
<syntaxhighlight lang=powershell>$char = [char] 'a'</syntaxhighlight> |
||
Then a simple cast to int yields the character code: |
Then a simple cast to int yields the character code: |
||
< |
<syntaxhighlight lang=powershell>$charcode = [int] $char # => 97</syntaxhighlight> |
||
This also works with Unicode: |
This also works with Unicode: |
||
< |
<syntaxhighlight lang=powershell>[int] [char] '☺' # => 9786</syntaxhighlight> |
||
For converting an integral character code into the actual character, a cast to char suffices: |
For converting an integral character code into the actual character, a cast to char suffices: |
||
< |
<syntaxhighlight lang=powershell>[char] 97 # a |
||
[char] 9786 # ☺</ |
[char] 9786 # ☺</syntaxhighlight> |
||
=={{header|Prolog}}== |
=={{header|Prolog}}== |
||
Line 1,925: | Line 1,925: | ||
A one-character string is used here to hold the character and a numerical character type is used to hold the character code. |
A one-character string is used here to hold the character and a numerical character type is used to hold the character code. |
||
The character type is either one or two bytes in size, depending on whether compiling for Ascii or Unicode respectively. |
The character type is either one or two bytes in size, depending on whether compiling for Ascii or Unicode respectively. |
||
< |
<syntaxhighlight lang=PureBasic>If OpenConsole() |
||
;Results are the same when compiled for Ascii or Unicode |
;Results are the same when compiled for Ascii or Unicode |
||
charCode.c = 97 |
charCode.c = 97 |
||
Line 1,935: | Line 1,935: | ||
Input() |
Input() |
||
CloseConsole() |
CloseConsole() |
||
EndIf</ |
EndIf</syntaxhighlight> |
||
This version should be compiled with Unicode setting and the source code to be encoded using UTF-8. |
This version should be compiled with Unicode setting and the source code to be encoded using UTF-8. |
||
< |
<syntaxhighlight lang=PureBasic>If OpenConsole() |
||
;UTF-8 encoding compiled for Unicode (UCS-2) |
;UTF-8 encoding compiled for Unicode (UCS-2) |
||
charCode.c = 960 |
charCode.c = 960 |
||
Line 1,948: | Line 1,948: | ||
Input() |
Input() |
||
CloseConsole() |
CloseConsole() |
||
EndIf</ |
EndIf</syntaxhighlight> |
||
=={{header|Python}}== |
=={{header|Python}}== |
||
Line 1,955: | Line 1,955: | ||
8-bit characters: |
8-bit characters: |
||
< |
<syntaxhighlight lang=python>print ord('a') # prints "97" |
||
print chr(97) # prints "a"</ |
print chr(97) # prints "a"</syntaxhighlight> |
||
Unicode characters: |
Unicode characters: |
||
< |
<syntaxhighlight lang=python>print ord(u'π') # prints "960" |
||
print unichr(960) # prints "π"</ |
print unichr(960) # prints "π"</syntaxhighlight> |
||
{{works with|Python|3.x}} |
{{works with|Python|3.x}} |
||
Here character is just a string of length 1 |
Here character is just a string of length 1 |
||
< |
<syntaxhighlight lang=python>print(ord('a')) # prints "97" (will also work in 2.x) |
||
print(ord('π')) # prints "960" |
print(ord('π')) # prints "960" |
||
print(chr(97)) # prints "a" (will also work in 2.x) |
print(chr(97)) # prints "a" (will also work in 2.x) |
||
print(chr(960)) # prints "π"</ |
print(chr(960)) # prints "π"</syntaxhighlight> |
||
=={{header|Quackery}}== |
=={{header|Quackery}}== |
||
Line 1,988: | Line 1,988: | ||
=={{header|R}}== |
=={{header|R}}== |
||
< |
<syntaxhighlight lang=R>ascii <- as.integer(charToRaw("hello world")); ascii |
||
text <- rawToChar(as.raw(ascii)); text</ |
text <- rawToChar(as.raw(ascii)); text</syntaxhighlight> |
||
=={{header|Racket}}== |
=={{header|Racket}}== |
||
< |
<syntaxhighlight lang=Racket>#lang racket |
||
(define (code ch) |
(define (code ch) |
||
Line 2,002: | Line 2,002: | ||
(printf "The unicode number ~a is the character ~s\n" n (integer->char n))) |
(printf "The unicode number ~a is the character ~s\n" n (integer->char n))) |
||
(char 97) |
(char 97) |
||
(char 955)</ |
(char 955)</syntaxhighlight> |
||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
(formerly Perl 6) |
(formerly Perl 6) |
||
Both Perl 5 and Raku have good Unicode support, though Raku attempts to make working with Unicode effortless. Note that even multi-byte emoji and characters outside the BMP are considered single characters. Also note: all of these routines are built into the base compiler. No need to load external libraries. See [[wp:Unicode_character_property#General_Category|Wikipedia: Unicode character properties]] for explanation of Unicode property. |
Both Perl 5 and Raku have good Unicode support, though Raku attempts to make working with Unicode effortless. Note that even multi-byte emoji and characters outside the BMP are considered single characters. Also note: all of these routines are built into the base compiler. No need to load external libraries. See [[wp:Unicode_character_property#General_Category|Wikipedia: Unicode character properties]] for explanation of Unicode property. |
||
<lang |
<syntaxhighlight lang=raku line>for 'AΑА𪚥🇺🇸👨👩👧👦'.comb { |
||
.put for |
.put for |
||
[ 'Character', |
[ 'Character', |
||
Line 2,037: | Line 2,037: | ||
]; |
]; |
||
say ''; |
say ''; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> Character: A |
<pre> Character: A |
||
Line 2,118: | Line 2,118: | ||
=={{header|RapidQ}}== |
=={{header|RapidQ}}== |
||
<syntaxhighlight lang=vb> |
|||
<lang vb> |
|||
Print Chr$(97) |
Print Chr$(97) |
||
Print Asc("a") |
Print Asc("a") |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Red}}== |
=={{header|Red}}== |
||
< |
<syntaxhighlight lang=Red>Red [] |
||
print to-integer first "a" ;; -> 97 |
print to-integer first "a" ;; -> 97 |
||
print to-integer #"a" ;; -> 97 |
print to-integer #"a" ;; -> 97 |
||
print to-binary "a" ;; -> #{61} |
print to-binary "a" ;; -> #{61} |
||
print to-char 97 ;; -> a |
print to-char 97 ;; -> a |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Retro}}== |
=={{header|Retro}}== |
||
<lang |
<syntaxhighlight lang=Retro>'c putc</syntaxhighlight> |
||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
REXX supports handling of characters with built-in functions (BIFs), whether it be hexadecimal, binary (bits), or decimal code(s). |
REXX supports handling of characters with built-in functions (BIFs), whether it be hexadecimal, binary (bits), or decimal code(s). |
||
===ASCII=== |
===ASCII=== |
||
< |
<syntaxhighlight lang=rexx>/*REXX program displays a char's ASCII code/value (or EBCDIC if run on an EBCDIC system)*/ |
||
yyy= 'c' /*assign a lowercase c to YYY. */ |
yyy= 'c' /*assign a lowercase c to YYY. */ |
||
yyy= "c" /* (same as above) */ |
yyy= "c" /* (same as above) */ |
||
Line 2,162: | Line 2,162: | ||
say ' dec code: ' c2d(yyy) /* decimal */ |
say ' dec code: ' c2d(yyy) /* decimal */ |
||
say ' bin code: ' x2b( c2x(yyy) ) /* binary (as a bit string) */ |
say ' bin code: ' x2b( c2x(yyy) ) /* binary (as a bit string) */ |
||
/*stick a fork in it, we're all done with display*/</ |
/*stick a fork in it, we're all done with display*/</syntaxhighlight> |
||
'''output''' |
'''output''' |
||
<pre> |
<pre> |
||
Line 2,178: | Line 2,178: | ||
===EBCDIC=== |
===EBCDIC=== |
||
< |
<syntaxhighlight lang=rexx>/* REXX */ |
||
yyy='c' /*assign a lowercase c to YYY */ |
yyy='c' /*assign a lowercase c to YYY */ |
||
yyy='83'x /*assign hexadecimal 83 to YYY */ |
yyy='83'x /*assign hexadecimal 83 to YYY */ |
||
Line 2,191: | Line 2,191: | ||
say c2x(yyy) /*displays the value of YYY in hexadecimal. */ |
say c2x(yyy) /*displays the value of YYY in hexadecimal. */ |
||
say c2d(yyy) /*displays the value of YYY in decimal. */ |
say c2d(yyy) /*displays the value of YYY in decimal. */ |
||
say x2b(c2x(yyy))/*displays the value of YYY in binary (bit string). */</ |
say x2b(c2x(yyy))/*displays the value of YYY in binary (bit string). */</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>a |
<pre>a |
||
Line 2,199: | Line 2,199: | ||
=={{header|Ring}}== |
=={{header|Ring}}== |
||
< |
<syntaxhighlight lang=ring> |
||
see ascii("a") + nl |
see ascii("a") + nl |
||
see char(97) + nl |
see char(97) + nl |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
In Ruby 1.9 characters are represented as length-1 strings; same as in Python. The previous "character literal" syntax <tt>?a</tt> is now the same as <tt>"a"</tt>. Subscripting a string also gives a length-1 string. There is now an "ord" method of strings to convert a character into its integer code. |
In Ruby 1.9 characters are represented as length-1 strings; same as in Python. The previous "character literal" syntax <tt>?a</tt> is now the same as <tt>"a"</tt>. Subscripting a string also gives a length-1 string. There is now an "ord" method of strings to convert a character into its integer code. |
||
< |
<syntaxhighlight lang=ruby>> "a".ord |
||
=> 97 |
=> 97 |
||
> 97.chr |
> 97.chr |
||
=> "a"</ |
=> "a"</syntaxhighlight> |
||
=={{header|Run BASIC}}== |
=={{header|Run BASIC}}== |
||
< |
<syntaxhighlight lang=runbasic>print chr$(97) 'prints a |
||
print asc("a") 'prints 97</ |
print asc("a") 'prints 97</syntaxhighlight> |
||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
< |
<syntaxhighlight lang=rust>use std::char::from_u32; |
||
fn main() { |
fn main() { |
||
Line 2,227: | Line 2,227: | ||
println!("{}", 'π' as u32); |
println!("{}", 'π' as u32); |
||
println!("{}", from_u32(960).unwrap()); |
println!("{}", from_u32(960).unwrap()); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>97 |
<pre>97 |
||
Line 2,235: | Line 2,235: | ||
=={{header|Sather}}== |
=={{header|Sather}}== |
||
< |
<syntaxhighlight lang=sather>class MAIN is |
||
main is |
main is |
||
#OUT + 'a'.int + "\n"; -- or |
#OUT + 'a'.int + "\n"; -- or |
||
Line 2,241: | Line 2,241: | ||
#OUT + CHAR::from_ascii_int(97) + "\n"; |
#OUT + CHAR::from_ascii_int(97) + "\n"; |
||
end; |
end; |
||
end;</ |
end;</syntaxhighlight> |
||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
Line 2,247: | Line 2,247: | ||
Scala supports unicode characters, but each character is UTF-16, so there is not a 1-to-1 relationship for supplementary character sets. |
Scala supports unicode characters, but each character is UTF-16, so there is not a 1-to-1 relationship for supplementary character sets. |
||
===In a REPL session=== |
===In a REPL session=== |
||
< |
<syntaxhighlight lang=scala>scala> 'a' toInt |
||
res2: Int = 97 |
res2: Int = 97 |
||
Line 2,257: | Line 2,257: | ||
scala> "\uD869\uDEA5" |
scala> "\uD869\uDEA5" |
||
res5: String = 𪚥</ |
res5: String = 𪚥</syntaxhighlight> |
||
===Full swing workout=== |
===Full swing workout=== |
||
Taken the supplemental character sets in account. |
Taken the supplemental character sets in account. |
||
< |
<syntaxhighlight lang=scala>import java.lang.Character._; import scala.annotation.tailrec |
||
object CharacterCode extends App { |
object CharacterCode extends App { |
||
Line 2,302: | Line 2,302: | ||
f"${"(" + UnicodeToInt(coll).toString}%8s) ${flags(coll)} ${getName(coll(0).toInt)} " |
f"${"(" + UnicodeToInt(coll).toString}%8s) ${flags(coll)} ${getName(coll(0).toInt)} " |
||
}.foreach(println) |
}.foreach(println) |
||
}</ |
}</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre style="height:20ex; overflow:scroll"> |
<pre style="height:20ex; overflow:scroll"> |
||
Line 2,377: | Line 2,377: | ||
=={{header|Scheme}}== |
=={{header|Scheme}}== |
||
< |
<syntaxhighlight lang=scheme>(display (char->integer #\a)) (newline) ; prints "97" |
||
(display (integer->char 97)) (newline) ; prints "a"</ |
(display (integer->char 97)) (newline) ; prints "a"</syntaxhighlight> |
||
=={{header|Seed7}}== |
=={{header|Seed7}}== |
||
< |
<syntaxhighlight lang=seed7>writeln(ord('a')); |
||
writeln(chr(97));</ |
writeln(chr(97));</syntaxhighlight> |
||
=={{header|SenseTalk}}== |
=={{header|SenseTalk}}== |
||
< |
<syntaxhighlight lang=sensetalk>put CharToNum("a") |
||
put NumToChar(97)</ |
put NumToChar(97)</syntaxhighlight> |
||
=={{header|SequenceL}}== |
=={{header|SequenceL}}== |
||
SequenceL natively supports ASCII characters.<br> |
SequenceL natively supports ASCII characters.<br> |
||
'''SequenceL Interpreter Session:''' |
'''SequenceL Interpreter Session:''' |
||
< |
<syntaxhighlight lang=sequencel>cmd:>asciiToInt('a') |
||
97 |
97 |
||
cmd:>intToAscii(97) |
cmd:>intToAscii(97) |
||
'a'</ |
'a'</syntaxhighlight> |
||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
< |
<syntaxhighlight lang=ruby>say 'a'.ord; # => 97 |
||
say 97.chr; # => 'a'</ |
say 97.chr; # => 'a'</syntaxhighlight> |
||
=={{header|Slate}}== |
=={{header|Slate}}== |
||
< |
<syntaxhighlight lang=slate>$a code. |
||
97 as: String Character.</ |
97 as: String Character.</syntaxhighlight> |
||
=={{header|Smalltalk}}== |
=={{header|Smalltalk}}== |
||
< |
<syntaxhighlight lang=smalltalk>($a asInteger) displayNl. "output 97" |
||
(Character value: 97) displayNl. "output a"</ |
(Character value: 97) displayNl. "output a"</syntaxhighlight> |
||
{{works with|Smalltalk/X}} |
{{works with|Smalltalk/X}} |
||
Ansi Smalltalk defines <tt>codePoint</tt> |
Ansi Smalltalk defines <tt>codePoint</tt> |
||
< |
<syntaxhighlight lang=smalltalk>Transcript showCR:$a codePoint. |
||
Transcript showCR:(Character codePoint:97). |
Transcript showCR:(Character codePoint:97). |
||
Transcript showCR:(98 asCharacter). |
Transcript showCR:(98 asCharacter). |
||
Line 2,416: | Line 2,416: | ||
'abcmøøse𝔘𝔫𝔦𝔠𝔬𝔡𝔢' do:[:ch | |
'abcmøøse𝔘𝔫𝔦𝔠𝔬𝔡𝔢' do:[:ch | |
||
Transcript showCR:ch codePoint |
Transcript showCR:ch codePoint |
||
]</ |
]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>97 |
<pre>97 |
||
Line 2,439: | Line 2,439: | ||
=={{header|SmileBASIC}}== |
=={{header|SmileBASIC}}== |
||
< |
<syntaxhighlight lang=smilebasic>PRINT CHR$(97) 'a |
||
PRINT ASC("a") '97</ |
PRINT ASC("a") '97</syntaxhighlight> |
||
=={{header|SNOBOL4}}== |
=={{header|SNOBOL4}}== |
||
Snobol implementations may or may not have built-in char( ) and ord ( ) or asc( ). |
Snobol implementations may or may not have built-in char( ) and ord ( ) or asc( ). |
||
These are based on examples in the Snobol4+ tutorial and work with the native (1-byte) charset. |
These are based on examples in the Snobol4+ tutorial and work with the native (1-byte) charset. |
||
< |
<syntaxhighlight lang=SNOBOL4> define('chr(n)') :(chr_end) |
||
chr &alphabet tab(n) len(1) . chr :s(return)f(freturn) |
chr &alphabet tab(n) len(1) . chr :s(return)f(freturn) |
||
chr_end |
chr_end |
||
Line 2,458: | Line 2,458: | ||
output = chr(65) |
output = chr(65) |
||
output = asc('A') |
output = asc('A') |
||
end</ |
end</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>A |
<pre>A |
||
Line 2,466: | Line 2,466: | ||
=={{header|SPL}}== |
=={{header|SPL}}== |
||
In SPL all characters are used in UTF-16LE encoding. |
In SPL all characters are used in UTF-16LE encoding. |
||
< |
<syntaxhighlight lang=spl>x = #.array("a") |
||
#.output("a -> ",x[1]," ",x[2]) |
#.output("a -> ",x[1]," ",x[2]) |
||
x = [98,0] |
x = [98,0] |
||
#.output("98 0 -> ",#.str(x))</ |
#.output("98 0 -> ",#.str(x))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,477: | Line 2,477: | ||
=={{header|Standard ML}}== |
=={{header|Standard ML}}== |
||
< |
<syntaxhighlight lang=sml>print (Int.toString (ord #"a") ^ "\n"); (* prints "97" *) |
||
print (Char.toString (chr 97) ^ "\n"); (* prints "a" *)</ |
print (Char.toString (chr 97) ^ "\n"); (* prints "a" *)</syntaxhighlight> |
||
=={{header|Stata}}== |
=={{header|Stata}}== |
||
The Mata '''ascii''' function transforms a string into a numeric vector of UTF-8 bytes. For instance: |
The Mata '''ascii''' function transforms a string into a numeric vector of UTF-8 bytes. For instance: |
||
< |
<syntaxhighlight lang=stata>: ascii("α") |
||
1 2 |
1 2 |
||
+-------------+ |
+-------------+ |
||
1 | 206 177 | |
1 | 206 177 | |
||
+-------------+</ |
+-------------+</syntaxhighlight> |
||
Where 206, 177 is the UTF-8 encoding of Unicode character 945 (GREEK SMALL LETTER ALPHA). |
Where 206, 177 is the UTF-8 encoding of Unicode character 945 (GREEK SMALL LETTER ALPHA). |
||
Line 2,493: | Line 2,493: | ||
ASCII characters are mapped to single bytes: |
ASCII characters are mapped to single bytes: |
||
< |
<syntaxhighlight lang=stata>: ascii("We the People") |
||
1 2 3 4 5 6 7 8 9 10 11 12 13 |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
||
+-------------------------------------------------------------------------------+ |
+-------------------------------------------------------------------------------+ |
||
1 | 87 101 32 116 104 101 32 80 101 111 112 108 101 | |
1 | 87 101 32 116 104 101 32 80 101 111 112 108 101 | |
||
+-------------------------------------------------------------------------------+</ |
+-------------------------------------------------------------------------------+</syntaxhighlight> |
||
Conversely, the '''char''' function transforms a byte vector into a string: |
Conversely, the '''char''' function transforms a byte vector into a string: |
||
< |
<syntaxhighlight lang=stata>: char((73,32,115,116,97,110,100,32,104,101,114,101)) |
||
I stand here</ |
I stand here</syntaxhighlight> |
||
=={{header|Swift}}== |
=={{header|Swift}}== |
||
The type that represent a Unicode code point is <code>UnicodeScalar</code>. |
The type that represent a Unicode code point is <code>UnicodeScalar</code>. |
||
You can initialize it with a string literal: |
You can initialize it with a string literal: |
||
< |
<syntaxhighlight lang=swift>let c1: UnicodeScalar = "a" |
||
println(c1.value) // prints "97" |
println(c1.value) // prints "97" |
||
let c2: UnicodeScalar = "π" |
let c2: UnicodeScalar = "π" |
||
println(c2.value) // prints "960"</ |
println(c2.value) // prints "960"</syntaxhighlight> |
||
Or, you can get it by iterating a string's unicode scalars view: |
Or, you can get it by iterating a string's unicode scalars view: |
||
< |
<syntaxhighlight lang=swift>let s1 = "a" |
||
for c in s1.unicodeScalars { |
for c in s1.unicodeScalars { |
||
println(c.value) // prints "97" |
println(c.value) // prints "97" |
||
Line 2,519: | Line 2,519: | ||
for c in s2.unicodeScalars { |
for c in s2.unicodeScalars { |
||
println(c.value) // prints "960" |
println(c.value) // prints "960" |
||
}</ |
}</syntaxhighlight> |
||
You can also initialize it from a <code>UInt32</code> integer: |
You can also initialize it from a <code>UInt32</code> integer: |
||
< |
<syntaxhighlight lang=swift>let i1: UInt32 = 97 |
||
println(UnicodeScalar(i1)) // prints "a" |
println(UnicodeScalar(i1)) // prints "a" |
||
let i2: UInt32 = 960 |
let i2: UInt32 = 960 |
||
println(UnicodeScalar(i2)) // prints "π"</ |
println(UnicodeScalar(i2)) // prints "π"</syntaxhighlight> |
||
=={{header|Tailspin}}== |
=={{header|Tailspin}}== |
||
Tailspin works with Unicode codepoints |
Tailspin works with Unicode codepoints |
||
< |
<syntaxhighlight lang=tailspin> |
||
'abc' -> $::asCodePoints -> !OUT::write |
'abc' -> $::asCodePoints -> !OUT::write |
||
'$#10;' -> !OUT::write |
'$#10;' -> !OUT::write |
||
'$#97;' -> !OUT::write |
'$#97;' -> !OUT::write |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,541: | Line 2,541: | ||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
< |
<syntaxhighlight lang=tcl># ASCII |
||
puts [scan "a" %c] ;# ==> 97 |
puts [scan "a" %c] ;# ==> 97 |
||
puts [format %c 97] ;# ==> a |
puts [format %c 97] ;# ==> a |
||
# Unicode is the same |
# Unicode is the same |
||
puts [scan "π" %c] ;# ==> 960 |
puts [scan "π" %c] ;# ==> 960 |
||
puts [format %c 960] ;# ==> π</ |
puts [format %c 960] ;# ==> π</syntaxhighlight> |
||
=={{header|TI-83 BASIC}}== |
=={{header|TI-83 BASIC}}== |
||
TI-83 BASIC provides no built in way to do this, so in all String<-->List routines and anything else which requires character codes, a workaround using inString( and sub( is used. |
TI-83 BASIC provides no built in way to do this, so in all String<-->List routines and anything else which requires character codes, a workaround using inString( and sub( is used. |
||
In this example, the code of 'A' is displayed, and then the character matching a user-defined code is displayed. |
In this example, the code of 'A' is displayed, and then the character matching a user-defined code is displayed. |
||
< |
<syntaxhighlight lang=ti83b>"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789→Str1 |
||
Disp inString(Str1,"A |
Disp inString(Str1,"A |
||
Input "CODE? ",A |
Input "CODE? ",A |
||
Disp sub(Str1,A,1</ |
Disp sub(Str1,A,1</syntaxhighlight> |
||
=={{header|TI-89 BASIC}}== |
=={{header|TI-89 BASIC}}== |
||
Line 2,565: | Line 2,565: | ||
The below program will display the character and code for any key pressed. Some keys do not correspond to characters and have codes greater than 255. |
The below program will display the character and code for any key pressed. Some keys do not correspond to characters and have codes greater than 255. |
||
The portion of the program actually implementing the task is marked with a line of “©”s. |
The portion of the program actually implementing the task is marked with a line of “©”s. |
||
< |
<syntaxhighlight lang=ti89b>Prgm |
||
Local k, s |
Local k, s |
||
ClrIO |
ClrIO |
||
Line 2,585: | Line 2,585: | ||
EndIf |
EndIf |
||
EndLoop |
EndLoop |
||
EndPrgm</ |
EndPrgm</syntaxhighlight> |
||
=={{header|Trith}}== |
=={{header|Trith}}== |
||
Characters are Unicode code points, so the solution is the same for Unicode characters as it is for ASCII characters: |
Characters are Unicode code points, so the solution is the same for Unicode characters as it is for ASCII characters: |
||
< |
<syntaxhighlight lang=trith>"a" ord print |
||
97 chr print</ |
97 chr print</syntaxhighlight> |
||
< |
<syntaxhighlight lang=trith>"π" ord print |
||
960 chr print</ |
960 chr print</syntaxhighlight> |
||
=={{header|TUSCRIPT}}== |
=={{header|TUSCRIPT}}== |
||
< |
<syntaxhighlight lang=tuscript>$$ MODE TUSCRIPT |
||
SET character ="a", code=DECODE (character,byte) |
SET character ="a", code=DECODE (character,byte) |
||
PRINT character,"=",code</ |
PRINT character,"=",code</syntaxhighlight> |
||
{{Out}}<pre>a=97</pre> |
{{Out}}<pre>a=97</pre> |
||
=={{header|uBasic/4tH}}== |
=={{header|uBasic/4tH}}== |
||
uBasic/4tH is an integer BASIC, just like Tiny BASIC. However, the function ORD() is supported, just as CHR(). The latter is only allowed within a PRINT statement. |
uBasic/4tH is an integer BASIC, just like Tiny BASIC. However, the function ORD() is supported, just as CHR(). The latter is only allowed within a PRINT statement. |
||
<lang>z = ORD("a") : PRINT CHR(z) ' Prints "a"</ |
<syntaxhighlight lang=text>z = ORD("a") : PRINT CHR(z) ' Prints "a"</syntaxhighlight> |
||
=={{header|UNIX Shell}}== |
=={{header|UNIX Shell}}== |
||
< |
<syntaxhighlight lang=bash> |
||
Aamrun$ printf "%d\n" \'a |
Aamrun$ printf "%d\n" \'a |
||
97 |
97 |
||
Line 2,611: | Line 2,611: | ||
a |
a |
||
Aamrun$ |
Aamrun$ |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Ursa}}== |
=={{header|Ursa}}== |
||
< |
<syntaxhighlight lang=ursa># outputs the character value for 'a' |
||
out (ord "a") endl console |
out (ord "a") endl console |
||
# outputs the character 'a' given its value |
# outputs the character 'a' given its value |
||
out (chr 97) endl console</ |
out (chr 97) endl console</syntaxhighlight> |
||
=={{header|Ursala}}== |
=={{header|Ursala}}== |
||
Character code functions are not built in but easily defined as reifications of |
Character code functions are not built in but easily defined as reifications of |
||
the character table. |
the character table. |
||
< |
<syntaxhighlight lang=Ursala>#import std |
||
#import nat |
#import nat |
||
Line 2,630: | Line 2,630: | ||
#cast %cnX |
#cast %cnX |
||
test = (chr97,asc`a)</ |
test = (chr97,asc`a)</syntaxhighlight> |
||
{{Out}}<pre>(`a,97)</pre> |
{{Out}}<pre>(`a,97)</pre> |
||
=={{header|VBA}}== |
=={{header|VBA}}== |
||
< |
<syntaxhighlight lang=vba>Debug.Print Chr(97) 'Prints a |
||
Debug.Print [Code("a")] ' Prints 97</ |
Debug.Print [Code("a")] ' Prints 97</syntaxhighlight> |
||
=={{header|VBScript}}== |
=={{header|VBScript}}== |
||
<syntaxhighlight lang=vb> |
|||
<lang vb> |
|||
'prints a |
'prints a |
||
WScript.StdOut.WriteLine Chr(97) |
WScript.StdOut.WriteLine Chr(97) |
||
Line 2,644: | Line 2,644: | ||
'prints 97 |
'prints 97 |
||
WScript.StdOut.WriteLine Asc("a") |
WScript.StdOut.WriteLine Asc("a") |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Vim Script}}== |
=={{header|Vim Script}}== |
||
The behavior of the two functions depends on the value of the option <code>encoding</code>. |
The behavior of the two functions depends on the value of the option <code>encoding</code>. |
||
< |
<syntaxhighlight lang=vim>"encoding is set to utf-8 |
||
echo char2nr("a") |
echo char2nr("a") |
||
"Prints 97 |
"Prints 97 |
||
echo nr2char(97) |
echo nr2char(97) |
||
"Prints a</ |
"Prints a</syntaxhighlight> |
||
=={{header|Visual Basic .NET}}== |
=={{header|Visual Basic .NET}}== |
||
< |
<syntaxhighlight lang=vbnet>Console.WriteLine(Chr(97)) 'Prints a |
||
Console.WriteLine(Asc("a")) 'Prints 97</ |
Console.WriteLine(Asc("a")) 'Prints 97</syntaxhighlight> |
||
=={{header|Vlang}}== |
=={{header|Vlang}}== |
||
< |
<syntaxhighlight lang=vlang>fn main() { |
||
println('a'[0]) // prints "97" |
println('a'[0]) // prints "97" |
||
println('π'[0]) // prints "207" |
println('π'[0]) // prints "207" |
||
Line 2,669: | Line 2,669: | ||
print('0x${c:x} ') |
print('0x${c:x} ') |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,681: | Line 2,681: | ||
=={{header|Wren}}== |
=={{header|Wren}}== |
||
Wren does not have a ''character'' type as such but one can use single character strings instead. Strings can contain any Unicode code point. |
Wren does not have a ''character'' type as such but one can use single character strings instead. Strings can contain any Unicode code point. |
||
< |
<syntaxhighlight lang=ecmascript>var cps = [] |
||
for (c in ["a", "π", "字", "🐘"]) { |
for (c in ["a", "π", "字", "🐘"]) { |
||
var cp = c.codePoints[0] |
var cp = c.codePoints[0] |
||
Line 2,691: | Line 2,691: | ||
var c = String.fromCodePoint(i) |
var c = String.fromCodePoint(i) |
||
System.print("%(i) = %(c)") |
System.print("%(i) = %(c)") |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,708: | Line 2,708: | ||
=={{header|XLISP}}== |
=={{header|XLISP}}== |
||
In a REPL: |
In a REPL: |
||
< |
<syntaxhighlight lang=scheme>[1] (INTEGER->CHAR 97) |
||
#\a |
#\a |
||
[2] (CHAR->INTEGER #\a) |
[2] (CHAR->INTEGER #\a) |
||
97</ |
97</syntaxhighlight> |
||
=={{header|XPL0}}== |
=={{header|XPL0}}== |
||
Line 2,720: | Line 2,720: | ||
character to an integer equal to its ASCII code. |
character to an integer equal to its ASCII code. |
||
< |
<syntaxhighlight lang=XPL0>IntOut(0, ^a); \(Integer Out) displays "97" on the console (device 0) |
||
ChOut(0, 97); \(Character Out) displays "a" on the console (device 0)</ |
ChOut(0, 97); \(Character Out) displays "a" on the console (device 0)</syntaxhighlight> |
||
=={{header|Z80 Assembly}}== |
=={{header|Z80 Assembly}}== |
||
The Z80 doesn't understand what ASCII codes are by itself. Most computers/systems that use it will have firmware that maps each code to its corresponding glyph. Printing a character given its code is trivial. On the Amstrad CPC: |
The Z80 doesn't understand what ASCII codes are by itself. Most computers/systems that use it will have firmware that maps each code to its corresponding glyph. Printing a character given its code is trivial. On the Amstrad CPC: |
||
< |
<syntaxhighlight lang=z80>LD A,'a' |
||
call &BB5a</ |
call &BB5a</syntaxhighlight> |
||
Printing a character code given a character takes slightly more work. You'll need to separate each hexadecimal digit of the ASCII code, convert each digit to ASCII, and print it. Once again, thanks to Keith of [[http://www.chibiakumas.com Chibiakumas]] for this code: |
Printing a character code given a character takes slightly more work. You'll need to separate each hexadecimal digit of the ASCII code, convert each digit to ASCII, and print it. Once again, thanks to Keith of [[http://www.chibiakumas.com Chibiakumas]] for this code: |
||
< |
<syntaxhighlight lang=z80>ShowHex: |
||
push af |
push af |
||
and %11110000 |
and %11110000 |
||
Line 2,747: | Line 2,747: | ||
jp PrintChar ;this is whatever routine prints to the screen on your system. |
jp PrintChar ;this is whatever routine prints to the screen on your system. |
||
; It must end in a "ret" and it must take the accumulator as its argument.</ |
; It must end in a "ret" and it must take the accumulator as its argument.</syntaxhighlight> |
||
=={{header|Zig}}== |
=={{header|Zig}}== |
||
< |
<syntaxhighlight lang=zig>const std = @import("std"); |
||
const debug = std.debug; |
const debug = std.debug; |
||
Line 2,780: | Line 2,780: | ||
debug.warn(" '{}' code: {} [hexa: U+{x}]\n", .{ slice, val, val }); |
debug.warn(" '{}' code: {} [hexa: U+{x}]\n", .{ slice, val, val }); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,800: | Line 2,800: | ||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
The character set is 8 bit ASCII (but doesn't care if you use UTF-8 or unicode characters). |
The character set is 8 bit ASCII (but doesn't care if you use UTF-8 or unicode characters). |
||
< |
<syntaxhighlight lang=zkl> "a".toAsc() //-->97 |
||
(97).toChar() //-->"a"</ |
(97).toChar() //-->"a"</syntaxhighlight> |
||
=={{header|Zoea}}== |
=={{header|Zoea}}== |
||
< |
<syntaxhighlight lang=Zoea> |
||
program: character_codes |
program: character_codes |
||
input: a |
input: a |
||
output: 97 |
output: 97 |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Zoea Visual}}== |
=={{header|Zoea Visual}}== |
||
Line 2,814: | Line 2,814: | ||
=={{header|ZX Spectrum Basic}}== |
=={{header|ZX Spectrum Basic}}== |
||
< |
<syntaxhighlight lang=zxbasic>10 PRINT CHR$ 97: REM prints a |
||
20 PRINT CODE "a": REM prints 97</ |
20 PRINT CODE "a": REM prints 97</syntaxhighlight> |
||
{{omit from|bc}} |
{{omit from|bc}} |