I want to convert the asci value of 9812 to ♔ UTF-8 string. How can it be done?
3 Answers
If it's a character in char range, then it's just:
Character.toString(9812).getBytes("UTF8")
If it's a code point larger than U+00FFFF, then you can use:
new String(Character.toChars(0x10400)).getBytes("UTF8")
If you just want a String, not the byte array with the UTF-8 representation, then omit getBytes.
2 Comments
[-16, -97, -104, -126] using both of your two approaches.You can use Character.toString:
String myString = Character.toString(9812);
Alternatively, if you only need a char, then you don't need any functions:
char myChar = 9812;
2 Comments
char type is obsolete, unable to handle even half of the characters defined in Unicode.The number 9812 in decimal is 2654 in Hexadecimal. There is an Open source library that can convert any string into Unicode sequence and vice-versa. So the following code will print your desired String.
System.out.println(StringUnicodeEncoderDecoder.decodeUnicodeSequenceToString("\\u2654"));
The output would be:
♔
Converting String to Unicode sequences would be as follows:
System.out.println(StringUnicodeEncoderDecoder.encodeStringToUnicodeSequence("Hello World"));
would result in this output:
\u0048\u0065\u006c\u006c\u006f\u0020\u0057\u006f\u0072\u006c\u0064
The library is called MgntUtils and could be found as Maven artifact here and at Github including jar, source code and Javadoc here. Just Javadoc for class StringUnicodeEncoderDecoder can be found here
9812is not an ASCII value, ASCII being limited to 128 numbers. The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)