Java getBytes() メソッド
getBytes() メソッドには2つの形式があります:
getBytes(String charsetName):指定された文字セットを使用して文字列をbyteシーケンスにエンコードし、結果を新しいbyte配列に格納します。
getBytes(): プラットフォームのデフォルト文字セットを使用して文字列をbyteシーケンスにエンコードし、結果を新しいbyte配列に格納します。
構文
public byte[] getBytes(String charsetName) throws UnsupportedEncodingException 或 public byte[] getBytes()
パラメータ
charsetName-- サポートされている文字セット名。
戻り値
byte配列を返します。
例
import java.io.*;
public class Test {
public static void main(String args[]) {
String Str1 = new String("サンプル");
try{
byte[] Str2 = Str1.getBytes();
System.out.println("戻り値:" + Str2 );
Str2 = Str1.getBytes( "UTF-8" );
System.out.println("戻り値:" + Str2 );
Str2 = Str1.getBytes( "ISO-8859-1" );
System.out.println("戻り値:" + Str2 );
} catch ( UnsupportedEncodingException e){
System.out.println("サポートされていない文字セット");
}
}
}
上記のプログラムの実行結果は次のとおりです:
返回值:[B@7852e922 返回值:[B@4e25154f 返回值:[B@70dea4e
その他の拡張
Java Stringクラス