System.out.println("default charset : "+Charset.defaultCharset());
String str = "abc你好";//string with UTF-8 charset
byte[] bytes = str.getBytes(Charset.forName("UTF-8"));//convert to byte array with UTF-8 encode
for (byte b : bytes)
{
System.out.print(b + " ");
}
System.out.println("\n");
for(byte b: bytesdefault)
{
System.out.print(b+" ");
}
System.out.println("\n");
byte[] bytesUnicode = str.getBytes(Charset.forName("Unicode"));
for(byte b: bytesUnicode)
{
System.out.print(b+" ");
}
System.out.print("\n");
System.out.println(bytesUnicode.length);
System.out.println("----------");
byte[] bytesISO88591 = str.getBytes(Charset.forName("ISO-8859-1"));
for(byte b: bytesISO88591)
{
System.out.print(b+" ");
}
System.out.println("");
String str12;
try {
str12 = new String(bytesISO88591,"ISO-8859-1");
System.out.print(str12);
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.out.print("\n");
try
{
String str1 = new String(bytes, "UTF-8");//to UTF-8 string
String str2 = new String(bytes, "ISO-8859-1");//to ISO-8859-1 string
String str3 = new String(bytes, "GBK");//to GBK string
System.out.println(str1);//abc你好
System.out.println(str2);//abc??????
System.out.println(str3);//abc浣犲ソ
System.out.println();
byte[] bytes2 = str2.getBytes(Charset.forName("ISO-8859-1"));
for (byte b : bytes2)
{
System.out.print(b + " ");
}
System.out.println();
String str22 = new String(bytes2, "UTF-8");
System.out.println(str22);//abc你好
System.out.println();
byte[] bytes3 = str3.getBytes(Charset.forName("GBK"));
for (byte b : bytes3)
{
System.out.print(b + " ");
}
System.out.println();
String str33 = new String(bytes3, "UTF-8");
System.out.println(str33);//abc你好
} catch (UnsupportedEncodingException e)
{
e.printStackTrace();
}
String str = "abc你好";//string with UTF-8 charset
byte[] bytes = str.getBytes(Charset.forName("UTF-8"));//convert to byte array with UTF-8 encode
for (byte b : bytes)
{
System.out.print(b + " ");
}
System.out.println("\n");
Charset.defaultCharset() 表示平台默认的编码格式;
byte[] bytes = str.getBytes(Charset.forName("UTF-8"))表示通过UTF-8把字符串解析成为字节数组;
str12 = new String(bytes,"ISO-8859-1");表示把字节数组以ISO-8859-1编码转化为字符串;
for(byte b: bytesdefault)
{
System.out.print(b+" ");
}
System.out.println("\n");
byte[] bytesUnicode = str.getBytes(Charset.forName("Unicode"));
for(byte b: bytesUnicode)
{
System.out.print(b+" ");
}
System.out.print("\n");
System.out.println(bytesUnicode.length);
System.out.println("----------");
byte[] bytesISO88591 = str.getBytes(Charset.forName("ISO-8859-1"));
for(byte b: bytesISO88591)
{
System.out.print(b+" ");
}
System.out.println("");
String str12;
try {
str12 = new String(bytesISO88591,"ISO-8859-1");
System.out.print(str12);
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.out.print("\n");
try
{
String str1 = new String(bytes, "UTF-8");//to UTF-8 string
String str2 = new String(bytes, "ISO-8859-1");//to ISO-8859-1 string
String str3 = new String(bytes, "GBK");//to GBK string
System.out.println(str1);//abc你好
System.out.println(str2);//abc??????
System.out.println(str3);//abc浣犲ソ
System.out.println();
byte[] bytes2 = str2.getBytes(Charset.forName("ISO-8859-1"));
for (byte b : bytes2)
{
System.out.print(b + " ");
}
System.out.println();
String str22 = new String(bytes2, "UTF-8");
System.out.println(str22);//abc你好
System.out.println();
byte[] bytes3 = str3.getBytes(Charset.forName("GBK"));
for (byte b : bytes3)
{
System.out.print(b + " ");
}
System.out.println();
String str33 = new String(bytes3, "UTF-8");
System.out.println(str33);//abc你好
} catch (UnsupportedEncodingException e)
{
e.printStackTrace();
}