String.getBytes()

时间:2021-07-09 16:00:28

package entity;

import java.io.UnsupportedEncodingException;

public class Test {

public static void main(String[] args) throws UnsupportedEncodingException {
byte[] by1 = "中".getBytes("utf-8");
System.out.println("utf-8=" + by1.length);

byte[] by2 = "中".getBytes("gbk");
System.out.println("gbk=" + by2.length);

byte[] by3 = "中".getBytes("ISO8859-1");
System.out.println("ISO8859-1=" + by3.length);
/*
* 运行结果 utf-8=3 gbk=2 ISO8859-1=1
*
*/
System.out.println("=========================================================");
System.out.println("utf-8="+new String(by1));
System.out.println("gbk="+new String(by2));
System.out.println("ISO8859-1="+new String(by3));
/**
* 运行结果
* 中 �� ?
*
*/
}
}