161

How can I convert, in Java, the ASCII code (which is an integer from [0, 255] range) to its corresponding ASCII character?

For example:

65  -> "A"
102 -> "f"
Giorgi Tsiklauri
  • 9,715
  • 8
  • 45
  • 66
Belgi
  • 14,542
  • 22
  • 58
  • 68

10 Answers10

285

Character.toString ((char) i);

thefourtheye
  • 233,700
  • 52
  • 457
  • 497
Chathuranga Chandrasekara
  • 20,548
  • 30
  • 97
  • 138
  • For MIDP 2 / CLDC 1.1 based platforms (which don't have `Character.toString(char)`, http://stackoverflow.com/a/6210938/923560 provides additional solutions. – Abdull Sep 22 '14 at 12:07
  • What is the reason for the `(char)` designation? In other words, why can't I just put `Character.toString(i);` ? (Java noob) – Adam Hughes Oct 09 '15 at 14:19
  • 1
    Note that this will not work for the `Integer` type, you will get a "java.lang.Integer cannot be cast to java.lang.Character" error. Add a cast to `int` first, e.g.: `Character.toString((char)(int)myInteger);` – gbmhunter Jun 07 '16 at 04:00
  • 1
    The `i` values (0-255) would be from the ISO-8859-1 character set. (The question asker declined to identify which "extended ASCII" [vague term] was wanted, except by accepting this answer.) – Tom Blodget Nov 03 '17 at 13:30
67

System.out.println((char)65); would print "A"

RylandAlmanza
  • 1,358
  • 1
  • 8
  • 14
38

String.valueOf(Character.toChars(int))

Assuming the integer is, as you say, between 0 and 255, you'll get an array with a single character back from Character.toChars, which will become a single-character string when passed to String.valueOf.

Using Character.toChars is preferable to methods involving a cast from int to char (i.e. (char) i) for a number of reasons, including that Character.toChars will throw an IllegalArgumentException if you fail to properly validate the integer while the cast will swallow the error (per the narrowing primitive conversions specification), potentially giving an output other than what you intended.

zjs
  • 668
  • 4
  • 6
  • Assuming that the integer is in the range 0 to 255 (as you state that you do ... and as the question specifies), it is unnecessary and suboptimal to use `toChars`. – Stephen C Oct 08 '11 at 00:51
  • 7
    You're completely correct that something like `Character.toString((char) i)` is faster than `String.valueOf(Character.toChars(i))`. Running a quick benchmark of converting 1,000,000 random integers in the given range (100 times, to be safe) on my machine gives an average time of 153.07 nanoseconds vs. 862.39 nanoseconds. However, in any interesting application, there will be far more important things to optimize. The added value of the safe, deterministic handling and ease of expanding outside the [0,255] range should it be required outweighs the minor performance hit. – zjs Oct 08 '11 at 02:26
14
int number = 65;
char c = (char)number;

it is a simple solution

athena
  • 279
  • 3
  • 11
7
    new String(new char[] { 65 })

You will end up with a string of length one, whose single character has the (ASCII) code 65. In Java chars are numeric data types.

wilmol
  • 1,429
  • 16
  • 22
Paul Cager
  • 1,910
  • 14
  • 21
2

An easier way of doing the same:

Type cast integer to character, let int n be the integer, then:

Char c=(char)n;
System.out.print(c)//char c will store the converted value.
Uri Agassi
  • 36,848
  • 14
  • 76
  • 93
1

One can iterate from a to z like this

int asciiForLowerA = 97;
int asciiForLowerZ = 122;
for(int asciiCode = asciiForLowerA; asciiCode <= asciiForLowerZ; asciiCode++){
    search(sCurrentLine, searchKey + Character.toString ((char) asciiCode));
}
1
    for (int i = 0; i < 256; i++) {
        System.out.println(i + " -> " + (char) i);
    }

    char lowercase = 'f';
    int offset = (int) 'a' - (int) 'A';
    char uppercase = (char) ((int) lowercase - offset);
    System.out.println("The uppercase letter is " + uppercase);

    String numberString = JOptionPane.showInputDialog(null,
            "Enter an ASCII code:",
            "ASCII conversion", JOptionPane.QUESTION_MESSAGE);

    int code = (int) numberString.charAt(0);
    System.out.println("The character for ASCII code "
            + code + " is " + (char) code);
-1

This is an example, which shows that by converting an int to char, one can determine the corresponding character to an ASCII code.

public class sample6
{
    public static void main(String... asf)
    {

        for(int i =0; i<256; i++)
        {
            System.out.println( i + ". " + (char)i);
        }
    }
}
Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
-1

upper answer only near solving the Problem. heres your answer:

Integer.decode(Character.toString(char c));