java编码

[代码] [Java]代码 import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;

public class Test {

	public static void main(String args[])
	{
		System.out.println("default charse http://www.fpshamen.com/linked/20130214.do; t : " 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();
		try
		{
			String str1 = new String(bytes, "UTF-8");//to UTF-8 string
			String str2 = new String(bytes, "GB18030");//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("GB18030"));
			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();
		}
	}
} http://www.fpnanchang.com/linked/20130214.do; 
posted @ 2013-02-15 02:30  chinadiy197601  阅读(178)  评论(0编辑  收藏  举报