「Java」String.getBytes()でバイト配列を生成する方法

説明
public byte[] getBytes(String charsetName) throws UnsupportedEncodingException
指定された文字セットを使用してこのStringをバイト・シーケンスにエンコードし、結果を新規バイト配列に格納します。
パラメータ:charsetName – サポートされているcharsetの名前
Javaコード

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package com.arkgame.study;
import java.io.UnsupportedEncodingException;
public class ByteDemo {
protected static final String charset = "UTF-8";
public static void main(String[] args) {
String str = "abc123";
byte[] cft;
try {
cft = str.getBytes(charset);
for (byte t : cft) {
System.out.println(t);
}
} catch (UnsupportedEncodingException e) {
// TODO 自動生成された catch ブロック
e.printStackTrace();
}
}
}
package com.arkgame.study; import java.io.UnsupportedEncodingException; public class ByteDemo { protected static final String charset = "UTF-8"; public static void main(String[] args) { String str = "abc123"; byte[] cft; try { cft = str.getBytes(charset); for (byte t : cft) { System.out.println(t); } } catch (UnsupportedEncodingException e) { // TODO 自動生成された catch ブロック e.printStackTrace(); } } }
package com.arkgame.study;

import java.io.UnsupportedEncodingException;

public class ByteDemo {
      protected static final String charset = "UTF-8";

      public static void main(String[] args) {
            String str = "abc123";
            byte[] cft;
            try {
                  cft = str.getBytes(charset);
                  for (byte t : cft) {
                        System.out.println(t);
                  }
            } catch (UnsupportedEncodingException e) {
                  // TODO 自動生成された catch ブロック
                  e.printStackTrace();
            }

      }

}

実行結果
97
98
99
49
50
51

Java

Posted by arkgame