编码方式转换

        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");

你可能感兴趣的:(JAVA)