Character codes: Difference between revisions
→{{header|Java}}
(Add lang example) |
|||
Line 1,182:
=={{header|Java}}==
In Java, a <code>char</code> is a 2-byte unsigned value, so it will fit within an 4-byte <code>int</code>.<br />
<br />
To convert a character to it's ASCII code, cast the <code>char</code> to an <code>int</code>.<br />
The following will yield <kbd>97</kbd>.
(int) 'a'
You could also specify a unicode hexadecimal value, using the <kbd>\u</kbd> escape sequence.
(int) '\u0061'
To convert an ASCII code to it's ASCII representation, cast the <code>int</code> value to a <code>char</code>.
<syntaxhighlight lang="java">
(char) 97
</syntaxhighlight>
<br />
Java also offers the <code>Character</code> class, comprised of several utilities for Unicode based operations.<br />
Here are a few examples.<br /><br />
Get the integer value represented by the ASCII character.<br />
The second parameter here, is the radix.
This will return an <code>int</code> with the value of <kbd>1</kbd>.
<syntaxhighlight lang="java">
Character.digit('1', 10)
</syntaxhighlight>
Inversely, get the ASCII representation of the integer.<br />
Again, the second parameter is the radix.
This will return a <code>char</code> with the value of '<kbd>1</kbd>'.
<syntaxhighlight lang="java">
Character.forDigit(1, 10)
</syntaxhighlight>
▲<syntaxhighlight lang="java">public class Foo {
▲}</syntaxhighlight>
▲<syntaxhighlight lang="java">public class Bar {
▲}</syntaxhighlight>
=={{header|JavaScript}}==
Here character is just a string of length 1
|