简体   繁体   English

在Java中无法将String转换为字节数组,反之亦然

[英]Cannot convert String to byte array and vice versa in Java

I am trying to convert a byte array to String . 我试图将byte array转换为String But the conversion alter the values. 但转换改变了价值观。 That means I cannot restore the byte array from the converted String . 这意味着我无法从转换后的String恢复byte array

byte[] array = {-64,-88,1,-2};
ByteArrayOutputStream out = new ByteArrayOutputStream();
out.write(array);
String result = out.toString("UTF-8");
byte[] array2 = result.getBytes("UTF-8");
// output of array2: {-17,-65,-67,-17}

It's a charset issue - utf-8 has more than 1 byte per char. 这是一个字符集问题 - utf-8每个字符有超过1个字节。 Try the same with some 1-byte charset like 尝试使用一些像1字节的字符集一样

String result = out.toString("ISO-8859-15");
byte[] array2 = result.getBytes("ISO-8859-15");

You have to use a fixed single byte encoding, like the one Jan suggested. 您必须使用固定的单字节编码,就像Jan建议的那样。 UTF-8 is a non-fixed encoding, that means, in certain cases you need more then one byte to encode a single code point. UTF-8是一种非固定编码,这意味着,在某些情况下,您需要多于一个字节来编码单个代码点。 This is one of this cases since you use negative numbers. 这是其中一种情况,因为您使用负数。 (See the table in the wiki page about utf-8 ) (参见wiki页面中关于utf-8的表格)

What was interesting for me was the fact, that after converting the second array to a string, the strings were identical but the underlying arrays where not. 对我来说有趣的是,在将第二个数组转换为字符串之后,字符串是相同的但是底层数组却没有。 But the point is, that the given character are not legit code points (or utf-8 representation of it) in which case the get replaced with the code point 65533, which in turn needs 3 bytes to be represented which explains the output: 但问题是,给定的字符不是合法的代码点(或者它的utf-8表示),在这种情况下,get代替了代码点65533,而代码点65533又需要3个字节来表示,这解释了输出:

[-17, -65, -67, -17, -65, -67, 1, -17, -65, -67]

The first two code points are represented as -17, -65, -67 and represent the illegal code point. 前两个代码点表示为-17,-65,-67,表示非法代码点。 The 1 represents a legit code point, so it "survived" the transformation and then last is again an illegal one. 1表示一个合法的代码点,因此它“幸存”了转换,然后最后再次是非法的。

I believe you can create a string out of an byte array by passing the array into the constructor like this 我相信你可以通过将数组传递给这样的构造函数来从字节数组中创建一个字符串

String test = new String(byte_array);

Also there's a method for String to convert a String to a byte-array that returns the array 还有一种String将String转换为返回数组的字节数组的方法

I hope that helped at least a bit 我希望至少有一点帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM