ChrisM
ChrisM

Reputation: 539

Java - Change int to ascii

Is there a way for java to convert int's to ascii symbols?

Upvotes: 53

Views: 211097

Answers (9)

Basil Bourque
Basil Bourque

Reputation: 338775

tl;dr

Character.toString( yourAsciiNumber ) 

Use Character#toString, not char. Like this:

String result = Character.toString( yourAsciiNumber ) ;

Example:

Character.toString( 97 )   // LATIN SMALL LETTER A

a

Character.toString( 128_567 )   // FACE WITH MEDICAL MASK

😷

char is legacy

The char type in Java is legacy, and is essentially broken. As a 16-bit value, char is incapable of representing most characters defined by Unicode.

This succeeds:

System.out.println( Character.toString( 128_567 ));  // Unicode code points handle full-range of Unicode characters.

😷

This fails:

System.out.println( ( char ) 128_567 );  // `char` fails with most Unicode characters. 

See code run live at IdeOne.com.

Code point

Use code point integer numbers to represent individual letters.

US-ASCII is a subset of Unicode. So, any US-ASCII number (0-127) is also a Unicode code point (0-1,114,111).

To change a code point number to a String object containing a single character, call Character#toString.

String x = Character.toString( 97 ) ;

a

See this code run live at IdeOne.com.

Upvotes: 2

Gautam kalamkar
Gautam kalamkar

Reputation: 1

The most simple way is to get integer and just use the casting operator Ex

int num = 33;
System.out.println((char) num);    //Outputs 33

//if you want to find the integer value of character instead.
//Just do the reverse

char ch = '%';
System.out.println((int) ch);

Upvotes: 0

besartm
besartm

Reputation: 568

The most simple way is using type casting:

public char toChar(int c) {
    return (char)c;
}

Upvotes: 0

user9229146
user9229146

Reputation:

You can convert a number to ASCII in java. example converting a number 1 (base is 10) to ASCII.

char k = Character.forDigit(1, 10);
System.out.println("Character: " + k);
System.out.println("Character: " + ((int) k));

Output:

Character: 1
Character: 49

Upvotes: 4

Travis
Travis

Reputation: 2026

There are many ways to convert an int to ASCII (depending on your needs) but here is a way to convert each integer byte to an ASCII character:

private static String toASCII(int value) {
    int length = 4;
    StringBuilder builder = new StringBuilder(length);
    for (int i = length - 1; i >= 0; i--) {
        builder.append((char) ((value >> (8 * i)) & 0xFF));
    }
    return builder.toString();
}

For example, the ASCII text for "TEST" can be represented as the byte array:

byte[] test = new byte[] { (byte) 0x54, (byte) 0x45, (byte) 0x53, (byte) 0x54 };

Then you could do the following:

int value = ByteBuffer.wrap(test).getInt(); // 1413829460
System.out.println(toASCII(value)); // outputs "TEST"

...so this essentially converts the 4 bytes in a 32-bit integer to 4 separate ASCII characters (one character per byte).

Upvotes: 6

Michael Aaron Safyan
Michael Aaron Safyan

Reputation: 95519

In Java, you really want to use Integer.toString to convert an integer to its corresponding String value. If you are dealing with just the digits 0-9, then you could use something like this:

private static final char[] DIGITS =
    {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};

private static char getDigit(int digitValue) {
   assertInRange(digitValue, 0, 9);
   return DIGITS[digitValue];
}

Or, equivalently:

private static int ASCII_ZERO = 0x30;

private static char getDigit(int digitValue) {
  assertInRange(digitValue, 0, 9);
  return ((char) (digitValue + ASCII_ZERO));
}

Upvotes: 1

Nadir
Nadir

Reputation: 715

In fact in the last answer String strAsciiTab = Character.toString((char) iAsciiValue); the essential part is (char)iAsciiValue which is doing the job (Character.toString useless)

Meaning the first answer was correct actually char ch = (char) yourInt;

if in yourint=49 (or 0x31), ch will be '1'

Upvotes: 1

RichardP
RichardP

Reputation: 532

If you first convert the int to a char, you will have your ascii code.

For example:

    int iAsciiValue = 9; // Currently just the number 9, but we want Tab character
    // Put the tab character into a string
    String strAsciiTab = Character.toString((char) iAsciiValue);

Upvotes: 18

Sean Patrick Floyd
Sean Patrick Floyd

Reputation: 298928

Do you want to convert ints to chars?:

int yourInt = 33;
char ch = (char) yourInt;
System.out.println(yourInt);
System.out.println(ch);
// Output:
// 33
// !

Or do you want to convert ints to Strings?

int yourInt = 33;
String str = String.valueOf(yourInt);

Or what is it that you mean?

Upvotes: 82

Related Questions