package Other;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
public class EncodeDemo {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// codeTrans2();
codeTrans();
}
public static void codeTrans2() throws IOException {
String s ="你好";
byte []buf = s.getBytes("GBk");//將字符串用GBK編碼為字節數組
String s1 = new String(buf,"ISO8859-1");//再將用GBK編碼的字節數組用ISO8859-1 解碼為新的字符串s1
System.out.println("s1 = "+s1);//使用平臺默認字符集(gbk)解析字符串s1 s1 = ????
byte []buf2 = s1.getBytes("ISO8859-1");//將s1用ISO8859-1進行再次解析,得到了只由GBK編碼的字節數組。
String s2 = new String(buf2,"GBK");//在將buf2用GBK進行解析,得到了由系統平臺默認的字符編碼集編碼的字符串。
System.out.println("s2 = "+s2);//使用平臺默認的字符編碼集進行打印。
}
public static void codeTrans() throws IOException {
String str = "你好";
//编码
// byte []buf = str.getBytes("GBK");//-60-29-70-61
byte []buf = str.getBytes("UTF-8");//-28-67-96-27-91-67
//解码
String s = new String(buf,"UTF-8");
System.out.println(s);
printByte(buf);
}
private static void printByte(byte[] buf) {
for(byte b : buf){
System.out.print(b);
}
}
}