gpt4 book ai didi

java - Java 中的 ISO-8859-1 编码是二进制安全的吗?

转载 作者:行者123 更新时间:2023-12-02 04:02:07 25 4
gpt4 key购买 nike

如果我使用 ISO-8859-1 编码将二进制流读入字符串,然后将其转换回二进制流,我是否总是会得到完全相同的字节?如果没有,我什么时候不会得到相同的字节?

public byte[] toStringAndBack(byte[] binaryData) throws Exception {
String s = new String(binaryData, "ISO-8859-1");
return s.getBytes("ISO-8859-1");
}

===编辑===

测试:

    byte[] d = {0, 1, 2, 3, 4, (byte)128, (byte)129, (byte)130}; // some not defined values
byte[] dd = toStringAndBack(d);
for (byte b : dd)
System.out.print((b&0xFF) + " ");

输出:

0 1 2 3 4 128 129 130

因此,即使未定义的字节似乎也可以正确转换。

最佳答案

constructor you're using说:

The behavior of this constructor when the given bytes are not valid in the given charset is unspecified.

因此理论上,对于 ISO-8859-1 未分配字符的任何值(例如 0-31 和 128-160),它可能会失败。

这意味着即使它在给定 JVM 的 String 实现(或 ISO-8859-1 的 Charset 实现)上运行,您也不能依赖它在另一个 JVM 上运行String/Charset 实现(无论这只是来自同一供应商的 JVM 的不同点版本,还是不同供应商的 JVM)。

关于java - Java 中的 ISO-8859-1 编码是二进制安全的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34786221/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com