get char value in java

后端 未结 8 1655
孤街浪徒
孤街浪徒 2020-12-18 20:04

How can I get the UTF8 code of a char in Java ? I have the char \'a\' and I want the value 97 I have the char \'é\' and I want the value 233

here is a table for more

8条回答
  •  情书的邮戳
    2020-12-18 20:46

    Those "UTF-8" codes are no such thing. They're actually just Unicode values, as per the Unicode code charts.

    So an 'é' is actually U+00E9 - in UTF-8 it would be represented by two bytes { 0xc3, 0xa9 }.

    Now to get the Unicode value - or to be more precise the UTF-16 value, as that's what Java uses internally - you just need to convert the value to an integer:

    char c = '\u00e9'; // c is now e-acute
    int i = c; // i is now 233
    

提交回复
热议问题