Character codes: Difference between revisions

Content added Content deleted
(Add LabVIEW)
m (Minor cleanup)
Line 11: Line 11:
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.</lang>
</lang>
Output:
Output:
<pre>
<pre>
Line 22: Line 21:
<lang ActionScipt>trace(String.fromCharCode(97)); //prints 'a'
<lang ActionScipt>trace(String.fromCharCode(97)); //prints 'a'
trace("a".charCodeAt(0));//prints '97'</lang>
trace("a".charCodeAt(0));//prints '97'</lang>

=={{header|Ada}}==
=={{header|Ada}}==
<lang ada>with Ada.Text_IO; use Ada.Text_IO;
<lang ada>with Ada.Text_IO; use Ada.Text_IO;
Line 33: Line 33:
a = 97
a = 97
</pre>
</pre>

=={{header|Aime}}==
=={{header|Aime}}==
<lang aime># prints "97"
<lang aime># prints "97"
Line 40: Line 41:
o_byte(97);
o_byte(97);
o_byte('\n');</lang>
o_byte('\n');</lang>

=={{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.
Line 63: Line 65:


=={{header|AWK}}==
=={{header|AWK}}==

AWK has not built-in way to convert a character into ASCII (or whatever) code; 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>
AWK has not built-in way to convert a character into ASCII (or whatever) code; 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>

<lang awk>function ord(c)
<lang awk>function ord(c)
{
{
Line 87: Line 87:
PRINT ASC(char) 'prints 97</lang>
PRINT ASC(char) 'prints 97</lang>


On the ZX Spectrum variable names must be a single letter:
{{works with|ZX Spectrum Basic}}
{{works with|ZX Spectrum Basic}}
<lang zxbasic>10 LET c = 97: REM c is a character code

On the ZX Spectrum variable names must be a single letter:

<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</lang>
</lang>


=={{header|BBC BASIC}}==
=={{header|BBC BASIC}}==
Line 155: Line 151:


=={{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.
<lang coffeescript>console.log 'a'.charCodeAt 0 # 97

console.log String.fromCharCode 97 # a</lang>
<lang coffeescript>
console.log 'a'.charCodeAt 0 # 97
console.log String.fromCharCode 97 # a
</lang>



=={{header|Common Lisp}}==
=={{header|Common Lisp}}==

<lang lisp>(princ (char-code #\a)) ; prints "97"
<lang lisp>(princ (char-code #\a)) ; prints "97"
(princ (code-char 97)) ; prints "a"</lang>
(princ (code-char 97)) ; prints "a"</lang>


=={{header|D}}==
=={{header|D}}==

Could be treated like C, but since the standard string type is UTF-8, let's be verbose.
Could be treated like C, but since the standard string type is UTF-8, let's be verbose.
<lang d>import std.stdio, std.utf;
<lang d>import std.stdio, std.utf;
Line 185: Line 174:


=={{header|Delphi}}==
=={{header|Delphi}}==

Example from Studio 2006.
Example from Studio 2006.
<lang delphi>program Project1;
<lang delphi>program Project1;
Line 205: Line 193:


Readln;
Readln;
end.
end.</lang>
</lang>


=={{header|DWScript}}==
=={{header|DWScript}}==

<lang delphi>PrintLn(Ord('a'));
<lang delphi>PrintLn(Ord('a'));
PrintLn(Chr(97));</lang>
PrintLn(Chr(97));</lang>


=={{header|E}}==
=={{header|E}}==

<lang e>? 'a'.asInteger()
<lang e>? 'a'.asInteger()
# value: 97
# value: 97
Line 222: Line 207:


=={{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:
<lang erlang>1> F = fun([X]) -> X end.
<lang erlang>1> F = fun([X]) -> X end.
Line 228: Line 212:
2> F("a").
2> F("a").
97</lang>
97</lang>

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>:
<lang erlang>3> $a.
<lang erlang>3> $a.
97</lang>
97</lang>

Unicode is fully supported since release R13A only.
Unicode is fully supported since release R13A only.


=={{header|Euphoria}}==
=={{header|Euphoria}}==

<lang Euphoria>printf(1,"%d\n", 'a') -- prints "97"
<lang Euphoria>printf(1,"%d\n", 'a') -- prints "97"
printf(1,"%s\n", 97) -- prints "a"</lang>
printf(1,"%s\n", 97) -- prints "a"</lang>
Line 245: Line 226:
printfn "%d" (int c)
printfn "%d" (int c)
printfn "%c" (char n)</lang>
printfn "%c" (char n)</lang>

Output
Output
<pre>
<pre>
Line 263: Line 243:


=={{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.
<lang fantom>fansh> 97.toChar

<lang fantom>
fansh> 97.toChar
a
a
fansh> 'a'.toInt
fansh> 'a'.toInt
97
97</lang>
</lang>


=={{header|Forth}}==
=={{header|Forth}}==
Line 286: Line 262:
=={{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 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.
<lang frink>
<lang frink>println[char["a"]] // prints 97
println[char["a"]] // prints 97
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!"</lang>
</lang>


=={{header|GAP}}==
=={{header|GAP}}==
Line 327: Line 301:
960
960
</pre>
</pre>

For the second part of the task, printing the character of a given code, the <tt>%c</tt> verb of <tt>fmt.Printf</tt> 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 <tt>%c</tt> verb of <tt>fmt.Printf</tt> 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.)
<lang go>b := byte(97)
<lang go>b := byte(97)
Line 347: Line 320:
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 golfscript>97[]+''+p</lang>
<lang golfscript>97[]+''+p</lang>

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:
<lang golfscript>'a')\;p
<lang golfscript>'a')\;p
Line 358: Line 330:
<lang groovy>printf ("%d\n", ('a' as char) as int)
<lang groovy>printf ("%d\n", ('a' as char) as int)
printf ("%c\n", 97)</lang>
printf ("%c\n", 97)</lang>

Output:
Output:
<pre>97
<pre>97
Line 364: Line 335:


=={{header|Haskell}}==
=={{header|Haskell}}==

<lang haskell>import Data.Char
<lang haskell>import Data.Char


Line 405: Line 375:
}
}
}</lang>
}</lang>

Java characters support Unicode:
Java characters support Unicode:
<lang java>public class Bar {
<lang java>public class Bar {
Line 422: Line 391:
<lang joy>'a ord.
<lang joy>'a ord.
97 chr.</lang>
97 chr.</lang>



=={{header|K}}==
=={{header|K}}==
Line 429: Line 397:


_ci 97 98 99 65 66 67
_ci 97 98 99 65 66 67
"abcABC"
"abcABC"</lang>
</lang>


=={{header|LabVIEW}}==
=={{header|LabVIEW}}==
{{VI snippet}}<br/>
{{VI snippet}}<br/>
[[File:LabVIEW_Character_codes.png]]
[[File:LabVIEW_Character_codes.png]]

=={{header|Liberty BASIC}}==
=={{header|Liberty BASIC}}==
<lang lb>charCode = 97
<lang lb>charCode = 97
Line 464: Line 432:
<lang Mathematica>ToCharacterCode["abcd"]
<lang Mathematica>ToCharacterCode["abcd"]
FromCharacterCode[{97}]</lang>
FromCharacterCode[{97}]</lang>

Result:
Result:
<pre>{97, 98, 99, 100}
<pre>{97, 98, 99, 100}


"a"</pre>
"a"</pre>



=={{header|MATLAB}} / {{header|Octave}}==
=={{header|MATLAB}} / {{header|Octave}}==

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:
Line 505: Line 470:


=={{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)

<lang metafont>message "enter a letter: ";
<lang metafont>message "enter a letter: ";
string a;
string a;
Line 554: Line 517:
<lang MUMPS>WRITE $ASCII("M")
<lang MUMPS>WRITE $ASCII("M")
WRITE $CHAR(77)</lang>
WRITE $CHAR(77)</lang>



=={{header|Objeck}}==
=={{header|Objeck}}==
<lang objeck>
<lang objeck>'a'->As(Int)->PrintLine();
'a'->As(Int)->PrintLine();
97->As(Char)->PrintLine();</lang>
97->As(Char)->PrintLine();
</lang>


=={{header|OCaml}}==
=={{header|OCaml}}==

<lang ocaml>Printf.printf "%d\n" (int_of_char 'a'); (* prints "97" *)
<lang ocaml>Printf.printf "%d\n" (int_of_char 'a'); (* prints "97" *)
Printf.printf "%c\n" (char_of_int 97); (* prints "a" *)</lang>
Printf.printf "%c\n" (char_of_int 97); (* prints "a" *)</lang>
Line 586: Line 545:
writeln(chr(97));</lang>
writeln(chr(97));</lang>


=={{header|Perl}}==
=={{header|Perl}} and {{header|Perl 6}}==
Here character is just a string of length 1
Here character is just a string of length 1
<lang perl>print ord('a'), "\n"; # prints "97"
<lang perl>print ord('a'), "\n"; # prints "97"
print chr(97), "\n"; # prints "a"</lang>
print chr(97), "\n"; # prints "a"</lang>

=={{header|Perl 6}}==
As Perl 5.


=={{header|PHP}}==
=={{header|PHP}}==
Line 612: Line 568:


=={{header|PL/I}}==
=={{header|PL/I}}==
<lang PL/I>
<lang PL/I>declare 1 u union,
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' */</lang>
</lang>


=={{header|PowerShell}}==
=={{header|PowerShell}}==
Line 630: Line 584:
<lang powershell>[char] 97 # a
<lang powershell>[char] 97 # a
[char] 9786 # ☺</lang>
[char] 9786 # ☺</lang>

=={{header|Prolog}}==
=={{header|Prolog}}==
SWI-Prolog has predefined predicate char_code/2.
SWI-Prolog has predefined predicate char_code/2.
Line 640: Line 595:


=={{header|PureBasic}}==
=={{header|PureBasic}}==

PureBasic allows compiling code so that it will use either Ascii or a Unicode (UCS-2) encoding for representing its string content. It also allows for the source code that is being compiled to be in either Ascii or UTF-8 encoding. 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.
PureBasic allows compiling code so that it will use either Ascii or a Unicode (UCS-2) encoding for representing its string content. It also allows for the source code that is being compiled to be in either Ascii or UTF-8 encoding. 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.
<lang PureBasic>If OpenConsole()
<lang PureBasic>If OpenConsole()
Line 694: Line 648:


=={{header|REXX}}==
=={{header|REXX}}==
REXX supports handling of characters with built-in functions,
REXX supports handling of characters with built-in functions, whether it be hexadecimal, binary (bits), or decimal codes.
<lang rexx>yyy='c' /*assign a lowercase c to YYY.*/
whether it be hexadecimal, binary (bits), or decimal codes.
<lang rexx>
yyy='c' /*assign a lowercase c to YYY.*/
yyy='34'x /*assign hexadecimal 34 to YYY.*/
yyy='34'x /*assign hexadecimal 34 to YYY.*/
yyy=x2c(34) /* (same as above) */
yyy=x2c(34) /* (same as above) */
Line 706: Line 658:
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). */</lang>
</lang>


=={{header|Ruby}}==
=={{header|Ruby}}==
Line 741: Line 692:


Without worrying about supplemental character sets:
Without worrying about supplemental character sets:

<lang scala>scala> 'a' toInt
<lang scala>scala> 'a' toInt
res9: Int = 97
res9: Int = 97
Line 748: Line 698:
res10: Char = a</lang>
res10: Char = a</lang>


Worrying about supplemental character sets, we need to test the "next"
Worrying about supplemental character sets, we need to test the "next" character as well:
character as well:

<lang scala>def charToInt(c: Char, next: Char): Option[Int] = (c, next) match {
<lang scala>def charToInt(c: Char, next: Char): Option[Int] = (c, next) match {
case _ if (c.isHighSurrogate && next.isLowSurrogate) => Some(java.lang.Character.toCodePoint(c, next))
case _ if (c.isHighSurrogate && next.isLowSurrogate) => Some(java.lang.Character.toCodePoint(c, next))
Line 760: Line 708:


=={{header|Scheme}}==
=={{header|Scheme}}==

<lang scheme>(display (char->integer #\a)) (newline) ; prints "97"
<lang scheme>(display (char->integer #\a)) (newline) ; prints "97"
(display (integer->char 97)) (newline) ; prints "a"</lang>
(display (integer->char 97)) (newline) ; prints "a"</lang>
Line 769: Line 716:


=={{header|Slate}}==
=={{header|Slate}}==
<lang slate>$a code.
<lang slate>$a code.
97 as: String Character.</lang>
97 as: String Character.</lang>


=={{header|Smalltalk}}==
=={{header|Smalltalk}}==

<lang smalltalk>($a asInteger) displayNl. "output 97"
<lang smalltalk>($a asInteger) displayNl. "output 97"
(Character value: 97) displayNl. "output a"</lang>
(Character value: 97) displayNl. "output a"</lang>


=={{header|SNOBOL4}}==
=={{header|SNOBOL4}}==

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.
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.

<lang SNOBOL4> define('chr(n)') :(chr_end)
<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)
Line 803: Line 746:


=={{header|Standard ML}}==
=={{header|Standard ML}}==

<lang sml>print (Int.toString (ord #"a") ^ "\n"); (* prints "97" *)
<lang sml>print (Int.toString (ord #"a") ^ "\n"); (* prints "97" *)
print (Char.toString (chr 97) ^ "\n"); (* prints "a" *)</lang>
print (Char.toString (chr 97) ^ "\n"); (* prints "a" *)</lang>


=={{header|Tcl}}==
=={{header|Tcl}}==

<lang tcl># ASCII
<lang tcl># ASCII
puts [scan "a" %c] ;# ==> 97
puts [scan "a" %c] ;# ==> 97
Line 822: Line 763:
Input "CODE? ",A
Input "CODE? ",A
Disp sub(Str1,A,1</lang>
Disp sub(Str1,A,1</lang>
=={{header|TI-89 BASIC}}==


=={{header|TI-89 BASIC}}==
The TI-89 uses an 8-bit charset/encoding which is similar to ISO-8859-1, but with more mathematical symbols and Greek letters. At least codes 14-31, 128-160, 180 differ. The ASCII region is unmodified. (TODO: Give a complete list.)
The TI-89 uses an 8-bit charset/encoding which is similar to ISO-8859-1, but with more mathematical symbols and Greek letters. At least codes 14-31, 128-160, 180 differ. The ASCII region is unmodified. (TODO: Give a complete list.)


Line 829: Line 770:


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 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.

<lang ti89b>Prgm
<lang ti89b>Prgm
Local k, s
Local k, s
Line 858: Line 798:
<lang trith>"π" ord print
<lang trith>"π" ord print
960 chr print</lang>
960 chr print</lang>

=={{header|TUSCRIPT}}==
=={{header|TUSCRIPT}}==
<lang tuscript>
<lang tuscript>$$ MODE TUSCRIPT
$$ MODE TUSCRIPT
SET character ="a", code=DECODE (character,byte)
SET character ="a", code=DECODE (character,byte)
PRINT character,"=",code
PRINT character,"=",code</lang>
</lang>
Output:
Output:
<pre>a=97</pre>
<pre>a=97</pre>

=={{header|Visual Basic .NET}}==

<lang vbnet>Console.WriteLine(Chr(97)) 'Prints a
Console.WriteLine(Asc("a")) 'Prints 97</lang>


=={{header|Ursala}}==
=={{header|Ursala}}==
Line 888: Line 822:
(`a,97)
(`a,97)
</pre>
</pre>

=={{header|Visual Basic .NET}}==
<lang vbnet>Console.WriteLine(Chr(97)) 'Prints a
Console.WriteLine(Asc("a")) 'Prints 97</lang>