以下代码更改字符串的每个字节并创建一个新字符串。字符串toByte和反向,如果字节被修改,则不是双射
public static String convert(String s) {
byte[] bytes = s.getBytes();
byte[] convert = new byte[bytes.length];
for (int i = 0; i < bytes.length; i++) {
convert[i] = (byte) ~bytes[i];
}
return new String(convert);
}
问题:为什么convert()不是双射的?
convert(convert("Test String")).equals("Test String") === false
一个'字符串'不只是一系列的任意字节。如果您想要将任意一系列的字节编码为文本,请使用Base64等。 –