java编码问题

来源:互联网 发布:数据恢复 深圳 编辑:程序博客网 时间:2024/06/13 19:41

Java编码问题

package com.mzsds.IO;import java.io.UnsupportedEncodingException;public class EncodeDemo {    public static void main(String[] args) throws UnsupportedEncodingException {        String s = "我是mzm";        byte[] bytes1= s.getBytes("GBK");        for (byte b : bytes1) {            System.out.print(Integer.toHexString(b & 0xff) + " ");            //gbk编码中文占用两个字节,英文占用一个        }        System.out.println();        byte[] bytes2=s.getBytes("UTF-8");        for (byte b : bytes2) {            System.out.print(Integer.toHexString(b & 0xff) + " ");            //utf-8编码中文占用三个字节,英文占用一个        }        //Java 是双字节编码 utf-16be        System.out.println();        byte[] bytes3=s.getBytes("UTF-16be");        for (byte b : bytes3) {            System.out.print(Integer.toHexString(b & 0xff) + " ");            //utf-16be编码中文占用两个个字节,英文占用两个字节        }        System.out.println();        String str1 = new String(bytes3);//使用项目默认的编码(这里是utf-8)        System.out.println(str1);        String str2 = new String(bytes3,"utf-16be");        System.out.println(str2);    }}
原创粉丝点击