package 测试想法的小示例代码;
import java.io.CharArrayWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
public class 关于字符串转码 {
private static String printByteArray(byte[] byteArray) {
String javaForm = "Java中的有符号表示形式: " + Arrays.toString(byteArray) + " ; ";
CharArrayWriter caw = new CharArrayWriter();
PrintWriter outs = new PrintWriter(caw);
outs.print("十六进制表示形式(有用的形式): ");
outs.print("[");
for (int i = 0; i < byteArray.length; i++) {
outs.format("0x%02X, ", byteArray[i]);
}
String hexForm = caw.toString();
hexForm = hexForm.substring(0, hexForm.length() - 2);
hexForm = hexForm + "]";
return javaForm + hexForm;
}
private static void printStringCode(String str) {
try {
System.err.println("\"" + str + "\"编码如下:");
System.out.println("\t(UTF-8): " + printByteArray(str.getBytes("UTF-8")));
System.out.println("\t(UTF-16): " + printByteArray(str.getBytes("UTF-16")));
System.out.println("\t(GBK): " + printByteArray(str.getBytes("GBK")));
System.out.println("\t(GB2312): " + printByteArray(str.getBytes("GB2312")));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return;
}
public static void main(String[] args) throws Exception {
// 0xCE,0xD2 是'我'字的GBK编码
// 因为Java编译器能识别的byte类型最大表示范围是-128~127,但由于0xCE和0xD2都大于127
// 故要先把这两个数字转为负数存进byte类型中
// (因为计算机中是以补码形式保存数据,故以负数存进byte数组后,实际保存的二进制数据还是0xCE和0xD2)
byte[] getParam = new byte[] { -((byte) (256L - 0xCE)), -((byte) (256L - 0xD2)) };
String value = new String(getParam, "ISO-8859-1"); // 将'我'字的GBK编码数据原样复制到到String对象中
byte[] b = value.getBytes("ISO-8859-1"); // 将value字符串对象中的数据原样复制到byte数组b中.
String newValue = new String(b, "GBK"); // 将数组b中的数据以GBK编码的方式转码成字符串对象的内部编码形式(GBK->UTF-16)
System.out.println(newValue);
printStringCode("我");
printStringCode("们");
}
}