I would like to get the Unicode character corresponding to the input int. You can assume that input int can be represented as char in Java.
Character.toString( 65 )
A
Character.toString( codePoint )You said:
I would like to get the unicode character corresponding to the input int
String character = Character.toString( codePoint ) ;
See these examples run live at IdeOne.com:
System.out.println( Character.toString( 65 ) ) ; // "A"
A
String faceWithMedicalMask = Character.toString( 128_567 ) ; // "😷" = FACE WITH MEDICAL MASK.
System.out.println( FaceWithMedicalMask ) ;
😷
Not all code points are assigned to encoded characters. Check that your input integer is indeed a valid code point.
int codePoint = 128_567 ;
String faceWithMedicalMask = null ;
if( Character.isValidCodePoint( codePoint ) ) {
faceWithMedicalMask = Character.toString( codePoint ) ;
} else {
… deal with invalid code point
}
char & Character typesYou said:
You can assume that input int can be represented as char in java.
No, you cannot assume that.
Most of the 149,813 characters defined in Unicode, and supported by Java, cannot be represented by the char type. As a 16-bit value, a char is physically incapable.
The char type has been essentially broken since Java 2, supplanted by code point support added in Java 5+.
int to charIf you insist on using char, against my recommendation, then know that you can cast an int number into a char value.
char c = (char) 66 ;
System.out.println( c ) ;
B
BEWARE: This only works for code points in the range of 0 to 65,535. Most Unicode characters have code points beyond that range, going up to 1,114,111.
That 0 to 65,535 range is known in Unicode as the Basic Multilingual Plane (BMP). Java provides a method to test for this range, Character.isBmpCodePoint(int codePoint). You can check before your cast:
if( Character.isBmpCodePoint( codePoint ) ) {
char c = (char) 66 ;
} else {
… deal with input out of range
}
Not all code points are assigned to encoded characters. Check that your input integer is indeed a valid code point.
if( Character.isBmpCodePoint( codePoint ) ) {
if( Character.isValidCodePoint( codePoint ) ) {
char c = (char) 66 ;
} else {
… deal with invalid code point
}
} else {
… deal with input out of range
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With