get char value in java

后端 未结 8 1616
孤街浪徒
孤街浪徒 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 21:01

    My method to do it is something like this:

    char c = 'c';
    int i = Character.codePointAt(String.valueOf(c), 0);
    // testing
    System.out.println(String.format("%c -> %d", c, i)); // c -> 99
    
    0 讨论(0)
  • 2020-12-18 21:03

    This produces good result:

    int a = 'a';
    System.out.println(a); // outputs 97
    

    Likewise:

    System.out.println((int)'é');
    

    prints out 233.

    Note that the first example only works for characters included in the standard and extended ASCII character sets. The second works with all Unicode characters. You can achieve the same result by multiplying the char by 1. System.out.println( 1 * 'é');

    0 讨论(0)
提交回复
热议问题