How to convert ASCII code (0-255) to its corresponding character?

JavaAscii

Java Problem Overview


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"

Java Solutions


Solution 1 - Java

Solution 2 - Java

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

Solution 3 - Java

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.

Solution 4 - Java

int number = 65;
char c = (char)number;

it is a simple solution

Solution 5 - Java

    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.

Solution 6 - Java

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.

Solution 7 - Java

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));
}

Solution 8 - Java

    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);

Solution 9 - Java

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);
		}
	}
}

Solution 10 - Java

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

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBelgiView Question on Stackoverflow
Solution 1 - JavaChathuranga ChandrasekaraView Answer on Stackoverflow
Solution 2 - JavaRylandAlmanzaView Answer on Stackoverflow
Solution 3 - JavazjsView Answer on Stackoverflow
Solution 4 - JavaathenaView Answer on Stackoverflow
Solution 5 - JavaPaul CagerView Answer on Stackoverflow
Solution 6 - JavaEntriple AardeeView Answer on Stackoverflow
Solution 7 - JavaTez KurmalaView Answer on Stackoverflow
Solution 8 - JavaKazım Fuat AkdemirView Answer on Stackoverflow
Solution 9 - JavaPrakash HadgalView Answer on Stackoverflow
Solution 10 - JavaMaximilian NotarView Answer on Stackoverflow