常用的编码格式

eclipse常用的编码格式有:US-ASCII、UTF-16、UTF-8、GBK等。

贴个讲的相当好的链接:http://www.qianxingzhem.com/post-1499.html

public class javatest {

    public static void main(String[] args) throws Exception{
        String s = "太阳ABC";
        /*
         * 因为eclipse默认的编码是GBK,所以byte1和byte2输出完全一样
         * GBK编码是英文用一个字节表示,汉字用两个字节
         */
        byte[] byte1 = s.getBytes();
        for (byte b : byte1) {
            System.out.print(Integer.toHexString(b & 0Xff) + " ");
        }
        System.out.println();
        byte[] byte2 = s.getBytes("GBK");
        for (byte b : byte2) {
            System.out.print(Integer.toHexString(b & 0Xff) + " ");
        }
        System.out.println();
        /*
         * ASCII码的表示方法,并不会将汉字表示出来
         */
        byte[] byte3 = s.getBytes("US-ASCII");
        for (byte b : byte3) {
            System.out.print(Integer.toHexString(b & 0Xff) + " ");
        }
        System.out.println();
        /*
         * UTF-16三个字节表示汉字,两个字节表示字母
         */
        byte[] byte4 = s.getBytes("UTF-16");
        for (byte b : byte4) {
            System.out.print(Integer.toHexString(b & 0Xff) + " ");
        }
        System.out.println();
        /*
         * UTF-16LE中两个字节表示汉字,两个字节表示字母
         */
        byte[] byte5 = s.getBytes("UTF-16LE");
        for (byte b : byte5) {
            System.out.print(Integer.toHexString(b & 0Xff) + " ");
        }
        System.out.println();
        /*
         * UTF-8中,三个字节表示汉字,两个字节表示字母
         */
        byte[] byte6 = s.getBytes("UTF-8");
        for (byte b : byte6) {
            System.out.print(Integer.toHexString(b & 0Xff) + " ");
        }
        System.out.println();
    }

}

 

posted @ 2015-10-18 22:00  千个太阳在手中  阅读(383)  评论(0编辑  收藏  举报