Let’s say I have this:
char registered = '®';
or an umlaut, or whatever unicode character. How could I get its code?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Just convert it to
int:In fact there’s an implicit conversion from
chartointso you don’t have to specify it explicitly as I’ve done above, but I would do so in this case to make it obvious what you’re trying to do.This will give the UTF-16 code unit – which is the same as the Unicode code point for any character defined in the Basic Multilingual Plane. (And only BMP characters can be represented as
charvalues in Java.) As Andrzej Doyle’s answer says, if you want the Unicode code point from an arbitrary string, useCharacter.codePointAt().Once you’ve got the UTF-16 code unit or Unicode code points, both of which are integers, it’s up to you what you do with them. If you want a string representation, you need to decide exactly what kind of representation you want. (For example, if you know the value will always be in the BMP, you might want a fixed 4-digit hex representation prefixed with
U+, e.g."U+0020"for space.) That’s beyond the scope of this question though, as we don’t know what the requirements are.