Skip to content

Instantly share code, notes, and snippets.

@ozuma
Last active April 27, 2018 13:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ozuma/0b3b3dda537bcaa22dcd13b46c17b946 to your computer and use it in GitHub Desktop.
Save ozuma/0b3b3dda537bcaa22dcd13b46c17b946 to your computer and use it in GitHub Desktop.
Unicode sample of Java
package com.example;
import java.io.UnsupportedEncodingException;
public class start {
public static void main(String[] args) throws UnsupportedEncodingException {
// TODO Auto-generated method stub
String str = "あいうえお";
// getBytes()は、特定の文字コードでのバイト列を取得する
// byte[] utf8bytes = str.getBytes("UTF-8");
// これは1バイトごと取り出す
byte[] utf8bytes = str.getBytes();
System.out.println(str);
for (byte b : utf8bytes) {
System.out.printf("%x", b);
// これを入れると1バイトごと取り出していることが分かる
// System.out.println(",");
}
System.out.println("");
System.out.println("-----------");
// Javaの内部表現はUTF-16なので、こうするとUTF-16のバイト列(そのままUnicodeのコードポイント)が出る
// これはHTMLで あ とかすればすぐ確認できる
// なおここでtoCharArray()は、1バイトごとでなく1文字ごと取り出すので、「あ」丸ごと取り出す。
for (char c : str.toCharArray()) {
System.out.println(c + " => 0x" + Integer.toHexString(c));
// System.out.printf(c + " => 0x" + "%x\n", (int)c);
}
System.out.println("-----------");
// Unicodeエスケープを変数名にする
int \u3042 = 10;
System.out.println(あ);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment