| 指定した文字コードで文字のバイト数を求める |
|
指定した文字コードで文字のバイト数を求めるにはString#getBytesメソッドを使用します。 getBytesメソッドの引数には文字コードを指定します。 Shift-JISとMS932は同じです。 また、引数を指定しない場合はプラットフォームの文字コードが指定されます。 |
| ソース |
public class Test {
public static void main(String[] args) throws Exception{
String str = "あ";
System.out.println(str.getBytes("UTF-8").length);
System.out.println(str.getBytes("Shift-JIS").length);
System.out.println(str.getBytes("MS932").length);
System.out.println(str.getBytes("EUC-JP").length);
}
}
|
| 実行結果 |
3 2 2 2 |