「Java入門」byte型の配列を文字列に変換する方法

javaコード

package zhongg;

import java.io.UnsupportedEncodingException;
import java.util.Arrays;

public class Byte2StrDemo {
public static void main(String[] args) throws UnsupportedEncodingException {
String aa = “テスト";

byte[] cftAA = aa.getBytes(“UTF-8");
byte[] cftBB = aa.getBytes(“SJIS");

// 指定された配列の文字列表現を返す
System.out.println(“UTF-8文字列:" + Arrays.toString(cftAA));
System.out.println(“SJIS文字列:" + Arrays.toString(cftBB));

// 指定された文字セットを使用する
String op2 = new String(cftAA, “UTF-8");
String op3 = new String(cftBB, “SJIS");

System.out.println(“新しいstring(UT-8):" + op2);
System.out.println(“新しいstrng(SJIS):" + op3);

}
}

結果

UTF-8文字列:[-29, -125, -122, -29, -126, -71, -29, -125, -120]
SJIS文字列:[-125, 101, -125, 88, -125, 103]
新しいstring(UT-8):テスト
新しいstrng(SJIS):テスト

Java

Posted by arkgame