get char value in java

后端 未结 8 1661
孤街浪徒
孤街浪徒 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:42

    Your question is unclear. Do you want the Unicode codepoint for a particular character (which is the example you gave), or do you want to translate a Unicode codepoint into a UTF-8 byte sequence?

    If the former, then I recommend the code charts at http://www.unicode.org/

    If the latter, then the following program will do it:

    public class Foo
    {
       public static void main(String[] argv)
       throws Exception
       {
          char c = '\u00E9';
          ByteArrayOutputStream bos = new ByteArrayOutputStream();
          OutputStreamWriter out = new OutputStreamWriter(bos, "UTF-8");
          out.write(c);
          out.flush();
          byte[] bytes = bos.toByteArray();
          for (int ii = 0 ; ii < bytes.length ; ii++)
             System.out.println(bytes[ii] & 0xFF);
       }
    }
    

    (there's also an online Unicode to UTF8 page, but I don't have the URL on this machine)

提交回复
热议问题