Jump to content

Character codes: Difference between revisions

m
Minor cleanup
(Add LabVIEW)
m (Minor cleanup)
Line 11:
assign c to <n> casting.
move <n> to n.
write: c, '=', n left-justified.</lang>
</lang>
Output:
<pre>
Line 22 ⟶ 21:
<lang ActionScipt>trace(String.fromCharCode(97)); //prints 'a'
trace("a".charCodeAt(0));//prints '97'</lang>
 
=={{header|Ada}}==
<lang ada>with Ada.Text_IO; use Ada.Text_IO;
Line 33:
a = 97
</pre>
 
=={{header|Aime}}==
<lang aime># prints "97"
Line 40 ⟶ 41:
o_byte(97);
o_byte('\n');</lang>
 
=={{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.
Line 63 ⟶ 65:
 
=={{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>
 
<lang awk>function ord(c)
{
Line 87:
PRINT ASC(char) 'prints 97</lang>
 
On the ZX Spectrum variable names must be a single letter:
{{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
30 PRINT CHR$(c): REM this prints a
40 PRINT CODE(d$): REM this prints 98</lang>
</lang>
 
=={{header|BBC BASIC}}==
Line 155 ⟶ 151:
 
=={{header|CoffeeScript}}==
 
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}}==
 
<lang lisp>(princ (char-code #\a)) ; prints "97"
(princ (code-char 97)) ; prints "a"</lang>
 
=={{header|D}}==
 
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;
Line 185 ⟶ 174:
 
=={{header|Delphi}}==
 
Example from Studio 2006.
<lang delphi>program Project1;
Line 205 ⟶ 193:
 
Readln;
end.</lang>
</lang>
 
=={{header|DWScript}}==
 
<lang delphi>PrintLn(Ord('a'));
PrintLn(Chr(97));</lang>
 
=={{header|E}}==
 
<lang e>? 'a'.asInteger()
# value: 97
Line 222 ⟶ 207:
 
=={{header|Erlang}}==
 
In Erlang, lists and strings are the same, only the representation changes. Thus:
<lang erlang>1> F = fun([X]) -> X end.
Line 228 ⟶ 212:
2> F("a").
97</lang>
 
If entered manually, one can also get ASCII codes by prefixing characters with <tt>$</tt>:
<lang erlang>3> $a.
97</lang>
 
Unicode is fully supported since release R13A only.
 
=={{header|Euphoria}}==
 
<lang Euphoria>printf(1,"%d\n", 'a') -- prints "97"
printf(1,"%s\n", 97) -- prints "a"</lang>
Line 245 ⟶ 226:
printfn "%d" (int c)
printfn "%c" (char n)</lang>
 
Output
<pre>
Line 263 ⟶ 243:
 
=={{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.
<lang fantom>fansh> 97.toChar
 
<lang fantom>
fansh> 97.toChar
a
fansh> 'a'.toInt
97</lang>
</lang>
 
=={{header|Forth}}==
Line 286 ⟶ 262:
=={{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.
<lang frink>println[char["a"]] // prints 97
println[char["a"]] // prints 97
println[char[97]] // prints a
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!"</lang>
</lang>
 
=={{header|GAP}}==
Line 327 ⟶ 301:
960
</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.)
<lang go>b := byte(97)
Line 347 ⟶ 320:
To convert a number to a string, we use the array to string coercion.
<lang golfscript>97[]+''+p</lang>
 
To convert a string to a number, we have a many options, of which the simplest and shortest are:
<lang golfscript>'a')\;p
Line 358 ⟶ 330:
<lang groovy>printf ("%d\n", ('a' as char) as int)
printf ("%c\n", 97)</lang>
 
Output:
<pre>97
Line 364 ⟶ 335:
 
=={{header|Haskell}}==
 
<lang haskell>import Data.Char
 
Line 405 ⟶ 375:
}
}</lang>
 
Java characters support Unicode:
<lang java>public class Bar {
Line 422 ⟶ 391:
<lang joy>'a ord.
97 chr.</lang>
 
 
=={{header|K}}==
Line 429 ⟶ 397:
 
_ci 97 98 99 65 66 67
"abcABC"</lang>
</lang>
 
=={{header|LabVIEW}}==
{{VI snippet}}<br/>
[[File:LabVIEW_Character_codes.png]]
 
=={{header|Liberty BASIC}}==
<lang lb>charCode = 97
Line 464 ⟶ 432:
<lang Mathematica>ToCharacterCode["abcd"]
FromCharacterCode[{97}]</lang>
 
Result:
<pre>{97, 98, 99, 100}
 
"a"</pre>
 
 
=={{header|MATLAB}} / {{header|Octave}}==
 
There are two built-in function that perform these tasks.
To convert from a number to a character use:
Line 505 ⟶ 470:
 
=={{header|Metafont}}==
 
Metafont handles only ''ASCII'' (even though codes beyond 127 can be given and used as real ASCII codes)
 
<lang metafont>message "enter a letter: ";
string a;
Line 554 ⟶ 517:
<lang MUMPS>WRITE $ASCII("M")
WRITE $CHAR(77)</lang>
 
 
=={{header|Objeck}}==
<lang objeck>'a'->As(Int)->PrintLine();
'a'97->As(IntChar)->PrintLine();</lang>
97->As(Char)->PrintLine();
</lang>
 
=={{header|OCaml}}==
 
<lang ocaml>Printf.printf "%d\n" (int_of_char 'a'); (* prints "97" *)
Printf.printf "%c\n" (char_of_int 97); (* prints "a" *)</lang>
Line 586 ⟶ 545:
writeln(chr(97));</lang>
 
=={{header|Perl}} and {{header|Perl 6}}==
Here character is just a string of length 1
<lang perl>print ord('a'), "\n"; # prints "97"
print chr(97), "\n"; # prints "a"</lang>
 
=={{header|Perl 6}}==
As Perl 5.
 
=={{header|PHP}}==
Line 612 ⟶ 568:
 
=={{header|PL/I}}==
<lang PL/I>declare 1 u union,
declare 1 u union,
2 c character (1),
2 i fixed binary (8) unsigned;
c = 'a'; put skip list (i); /* prints 97 */
i = 97; put skip list (c); /* prints 'a' */</lang>
</lang>
 
=={{header|PowerShell}}==
Line 630 ⟶ 584:
<lang powershell>[char] 97 # a
[char] 9786 # ☺</lang>
 
=={{header|Prolog}}==
SWI-Prolog has predefined predicate char_code/2.
Line 640 ⟶ 595:
 
=={{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.
<lang PureBasic>If OpenConsole()
Line 694 ⟶ 648:
 
=={{header|REXX}}==
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=x2c(34) /* (same as above) */
Line 706 ⟶ 658:
say c2x(yyy) /*displays the value of YYY in hexadecimal. */
say c2d(yyy) /*displays the value of YYY in decimal. */
say x2b(c2x(yyy)) /*displays the value of YYY in binary (bit string). */</lang>
</lang>
 
=={{header|Ruby}}==
Line 741 ⟶ 692:
 
Without worrying about supplemental character sets:
 
<lang scala>scala> 'a' toInt
res9: Int = 97
Line 748 ⟶ 698:
res10: Char = a</lang>
 
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 {
case _ if (c.isHighSurrogate && next.isLowSurrogate) => Some(java.lang.Character.toCodePoint(c, next))
Line 760 ⟶ 708:
 
=={{header|Scheme}}==
 
<lang scheme>(display (char->integer #\a)) (newline) ; prints "97"
(display (integer->char 97)) (newline) ; prints "a"</lang>
Line 769 ⟶ 716:
 
=={{header|Slate}}==
<lang slate>$a code.
97 as: String Character.</lang>
 
=={{header|Smalltalk}}==
 
<lang smalltalk>($a asInteger) displayNl. "output 97"
(Character value: 97) displayNl. "output a"</lang>
 
=={{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.
 
<lang SNOBOL4> define('chr(n)') :(chr_end)
chr &alphabet tab(n) len(1) . chr :s(return)f(freturn)
Line 803 ⟶ 746:
 
=={{header|Standard ML}}==
 
<lang sml>print (Int.toString (ord #"a") ^ "\n"); (* prints "97" *)
print (Char.toString (chr 97) ^ "\n"); (* prints "a" *)</lang>
 
=={{header|Tcl}}==
 
<lang tcl># ASCII
puts [scan "a" %c] ;# ==> 97
Line 822 ⟶ 763:
Input "CODE? ",A
Disp sub(Str1,A,1</lang>
=={{header|TI-89 BASIC}}==
 
=={{header|PerlTI-89 6BASIC}}==
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 ⟶ 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.
 
<lang ti89b>Prgm
Local k, s
Line 858 ⟶ 798:
<lang trith>"π" ord print
960 chr print</lang>
 
=={{header|TUSCRIPT}}==
<lang tuscript>$$ MODE TUSCRIPT
$$ MODE TUSCRIPT
SET character ="a", code=DECODE (character,byte)
PRINT character,"=",code</lang>
</lang>
Output:
<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}}==
Line 888 ⟶ 822:
(`a,97)
</pre>
 
=={{header|TI-89Visual BASICBasic .NET}}==
<lang vbnet>Console.WriteLine(Chr(97)) 'Prints a
Console.WriteLine(Asc("a")) 'Prints 97</lang>
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.