编码方式转换

来源:互联网 发布:linux初学者版本 编辑:程序博客网 时间:2024/06/06 02:40
        String str1 = new String("网");        try {            byte[] strby = str1.getBytes("gb2312");            String Str2 = new String(strby, "gb2312");            System.out.println(Str2);        } catch (UnsupportedEncodingException e) {            e.printStackTrace();        }  

两种方式:GB2312方式和UTF-8,之间不能直接转换,

编码方式可以获得对应的byte数组:str1.getBytes(); 

byte之间可以转换,str1.getBytes("gb2312")将str1的byte数组转成gb2312形式,再转成String类型,注意如果缺省的话默认为UTF-8形式,故String Str2 = new String(strby, "gb2312");

原创粉丝点击