164

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"
6
  • 3
    this is not a duplicate as mentioned above. This is not conversion from integer but from char (ascii) Oct 8, 2011 at 0:32
  • 11
    Not a duplicate of "How to convert from int to String?"... anyway, FWIW, ASCII is only 7-bits with values [0, 127] ;-)
    – user166390
    Oct 8, 2011 at 0:33
  • 1
    @phooji I think that that post sais how to convert 1->"1" etc'
    – Belgi
    Oct 8, 2011 at 0:35
  • 1
    @pst - it's extended ASII ;-)
    – Belgi
    Oct 8, 2011 at 0:36
  • 3
    @Belgi - you'll need to explicitly state your encoding if you want to correctly transcode values 128-255. The term "extended ASCII" is not meaningful.
    – McDowell
    Oct 8, 2011 at 9:45

10 Answers 10

290

Character.toString ((char) i);

4
  • For MIDP 2 / CLDC 1.1 based platforms (which don't have Character.toString(char), stackoverflow.com/a/6210938/923560 provides additional solutions.
    – Abdull
    Sep 22, 2014 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) Oct 9, 2015 at 14:19
  • 2
    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 7, 2016 at 4: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.) Nov 3, 2017 at 13:30
67

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

2
  • 3
    REPL tip: If you happen to be using JShell (Java 9) you can omit the System.out. Just type (char) 65 to find out what character it is.
    – user201891
    Jan 23, 2018 at 23:55
  • 3
    Question is not about printing but converting. Sep 11, 2019 at 10:51
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.

2
  • 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 8, 2011 at 0: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 8, 2011 at 2:26
15
int number = 65;
char c = (char)number;

it is a simple solution

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.

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.
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
  • Explain your answer please
    – want2learn
    Nov 2, 2017 at 16:57
-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);
        }
    }
}
-1

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

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.