编码在解码逆着顺序就可以了吗?

编码的程序是


String tempS = new String(resutlBytesData[p], "UTF-16BE");

byte[] btmp = tempS.getBytes("GBK");

System.out.println(Base64.encode(btmp));

然后我解码的顺序是


byte[] b1 = Base64.decode(s1);

String ss1 = new String(b1,"GBK");

byte[] bb1 = ss1.getBytes("UTF-16BE");

bb1解出来后是resutlBytesData[p],但是不对?哪里出错了吗? 完整的代码如下


public class anotherTest {

public static void main(String[] args) throws UnsupportedEncodingException {

    byte[] bytes = {6, 8, 4, 0, 1, 2, 1, 117, 49, 78, -114, 78, 14, 123, 44, 78, 9, 101, -71, 84, 8, 79, 92, -116, 3, 101, 116, -1, 12, 102, -126, 80, 92, -117, -35, -115, 57, 101, 47, 78, -40, 81, 81, 99, 98, 98, 75, 103, 58, 101, 47, 78, -40, 117, 53, 91, 80, 82, 56, -1, 8, 84, 43, 81, 108, 78, -92, 78, 0, 83, 97, -112, 26, 117, 53, 91, 80, 82, 56, -1, 9, 78, 26, 82, -95, 48, 2, -117, -9, 96, -88, -112, 9, 98, -23, 81, 118, 78, -42, 83, -17, -115, 45, 78, 112, 78, -89, 84, -63, 48, 2, -117, -35, -115, 57, 101, 47, 78, -40, 83, -17, -115, 45, 78, 112, 118, -124};

    System.out.println("解码前:"+Arrays.toString(bytes));

    String tempS = new String(bytes, "UTF-16BE");//得到的tempS就是乱码了

    byte[] btmp = tempS.getBytes("GBK");

    String result = Base64.encode(btmp);


    byte[] rBtemp = Base64.decode(result);

    String rTempS = new String(rBtemp,"GBK");

    byte[] rBytes = rTempS.getBytes("UTF-16BE");

    System.out.println("解码后:"+Arrays.toString(rBytes));

}

}


慕姐8265434
浏览 320回答 1
1回答

收到一只叮咚

bytes数组UTF-16BE编码后得到的String乱码,说明bytes数组的初始编码不是UTF-16BE,这个编码过程失败了。另外UTF-16BE与GBK的编码规则不同,是不兼容的,这样转换后,也会出错。UTF-16编码是定长编码。统一以2个byte表示所有单个字符,不管是英文还是中文。UTF-8是变长编码,ASCII字符以一个byte表示,中文字符以3个byte表示。GB2312也是变长编码,ASCII字符以一个byte表示,中文字符以2个byte表示,GBK是GB2312的扩展,扩展了编码范围。不同的编码规则,不同的编码范围,导致字符被翻译成字节后,会有信息丢失;同理,也会导致编码后的字节流被重组成字符后,出现乱码。PS:可以参考这篇文章,还不错:深入分析Java中的中文编码问题
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java