Java – Convert Int to Char
In Java, an int can be converted to a char in different ways depending on what the integer represents. Use an explicit (char) cast when the integer contains a UTF-16 character value. Use Character.forDigit() when the integer represents a numeric digit that should become a digit character such as '8'.
These operations are not interchangeable. For example, casting the integer 8 to char does not produce the character '8'. It produces the character whose UTF-16 value is 8. To obtain '8', use Character.forDigit(8, 10).
1. Convert int to char in Java using narrow casting
A Java int is a 32-bit signed integer, while char is an unsigned 16-bit value representing a UTF-16 code unit. Because an int can contain values that do not fit directly in a char, Java requires an explicit cast.
The syntax for converting an int to char is:
char character = (char) integerValue;
The cast keeps the lower 16 bits of the integer and interprets them as a char. Therefore, this approach should be used when the integer value is intended to represent a UTF-16 character value.
In the following example, the integer variable i contains the value 69. Casting it to char produces E, because Unicode code point U+0045 has the decimal value 69.
Java Program
/**
* Java Program - Convert Int to Char
*/
public class IntToChar {
public static void main(String[] args) {
int i = 69;
char c = (char) i;
System.out.println(c);
}
}
Output
E
How Java maps an integer value to a char
For character values in the Basic Multilingual Plane, an integer can be cast directly to char. The following program converts several integer values to their corresponding characters.
public class IntToCharValues {
public static void main(String[] args) {
int value1 = 65;
int value2 = 97;
int value3 = 8364;
System.out.println((char) value1);
System.out.println((char) value2);
System.out.println((char) value3);
}
}
Output
A
a
€
Here, decimal values 65, 97, and 8364 correspond to the characters A, a, and €, respectively.
What happens when the int value is outside the Java char range?
A Java char can represent values from 0 through 65535. When an int outside this range is explicitly cast to char, Java discards all but the lowest 16 bits. This can change the numeric value.
public class IntToCharRange {
public static void main(String[] args) {
int number = 65536;
char character = (char) number;
System.out.println((int) character);
}
}
Output
0
The value 65536 cannot be represented as a single char. After the narrowing conversion, the resulting numeric char value is 0. For this reason, check the intended range before casting arbitrary integers to char.
Why Java reports possible lossy conversion from int to char
Assigning a general int variable directly to a char causes a compile-time error because an int has a wider numeric range. Java does not perform this narrowing conversion implicitly.
int number = 69;
char character = number; // compile-time error
Use an explicit cast when you know that the integer should be interpreted as a character value:
char character = (char) number;
2. Convert an integer digit to char using Character.forDigit()
Character.forDigit() is a static method of the Character class. It returns the character representation of a digit for the specified radix, or number base.
Its basic syntax is:
char character = Character.forDigit(digit, radix);
For decimal digits, use radix 10. For hexadecimal digits, use radix 16. For example, the digit value 12 in hexadecimal is represented by the character c.
In this example, we shall use Character.forDigit() method to get the representation of the digit in the given radix.
For example, the digit representation of 12 in hexa-decimal (radix 16) system is c.
Java Program
/**
* Java Program - Convert Int to Char
*/
public class IntToChar {
public static void main(String[] args) {
char c1 = Character.forDigit(12, 16);
System.out.println(c1);
char c2 = Character.forDigit(8, 10);
System.out.println(c2);
}
}
Output
c
8
Convert an int from 0 to 9 into the corresponding digit char
If an integer contains a decimal digit from 0 to 9, Character.forDigit(number, 10) converts it to the corresponding character. For example, integer 5 becomes character '5'.
public class DigitToChar {
public static void main(String[] args) {
int number = 5;
char digit = Character.forDigit(number, 10);
System.out.println(digit);
}
}
Output
5
This is different from (char) 5. The cast treats 5 as a character value, whereas Character.forDigit(5, 10) returns the printable digit character '5'.
Invalid digit and radix values with Character.forDigit()
Character.forDigit() returns the null character '\0' when the digit is invalid for the specified radix or when the radix itself is outside the supported range. For example, decimal radix 10 accepts digit values from 0 through 9, so passing 12 with radix 10 does not produce the two characters "12".
public class InvalidDigitToChar {
public static void main(String[] args) {
char c = Character.forDigit(12, 10);
System.out.println((int) c);
}
}
Output
0
A Java int representing a Unicode code point may require more than one char
Not every Unicode code point fits in one Java char. A char stores one UTF-16 code unit, so supplementary Unicode characters require a pair of char values called a surrogate pair.
If an int contains a Unicode code point greater than 0xFFFF, do not simply cast it to char. Use Character.toChars() and create a String instead.
public class UnicodeCodePointToString {
public static void main(String[] args) {
int codePoint = 0x1F600;
String text = new String(Character.toChars(codePoint));
System.out.println(text);
}
}
Output
😀
Choosing the correct int-to-char conversion in Java
| Integer represents | Recommended conversion | Example |
|---|---|---|
| A UTF-16 character value | (char) value | (char) 69 gives 'E' |
| A decimal digit from 0 to 9 | Character.forDigit(value, 10) | Character.forDigit(8, 10) gives '8' |
| A digit in another radix | Character.forDigit(value, radix) | Character.forDigit(12, 16) gives 'c' |
| A supplementary Unicode code point | Character.toChars(value) | Create a String from the returned char array |
Use explicit casting only when the integer is intended to be interpreted as a UTF-16 char value. Use Character.forDigit() for numeric digit conversion, and use Character.toChars() when a Unicode code point may require two UTF-16 code units.
In this Java Tutorial, we learned how to write Java programs to convert an Int to Char.
TutorialKart.com