如何在Java中将字节数组转换为String? [重复]

时间:2022-10-30 15:45:39

This question already has an answer here:

这个问题在这里已有答案:

How can I convert a array of bytes to String without conversion?.

如何在不转换的情况下将字节数组转换为String?

I tried:

我试过了:

  String doc=new String( bytes);

But the doc file is not the same than the bytes (the bytes are binary information). For example:

但doc文件与字节不同(字节是二进制信息)。例如:

  String doc=new String( bytes);
  byte[] bytes2=doc.getBytes();

bytes and bytes2 are different.

bytes和bytes2是不同的。

PS: UTF-8 Does not work because it convert some bytes in different values. I tested and it does not work.

PS:UTF-8不起作用,因为它转换了不同值的一些字节。我测试过它不起作用。

PS2: And no, I don't want BASE64.

PS2:不,我不想要BASE64。

3 个解决方案

#1


16  

You need to specify the encoding you want e.g. for UTF-8

您需要指定所需的编码,例如对于UTF-8

String doc = ....
byte[] bytes = doc.getBytes("UTF-8");
String doc2 = new String(bytes, "UTF-8");

doc and doc2 will be the same.

doc和doc2将是相同的。

To decode a byte[] you need to know what encoding was used to be sure it will decode correctly.

要解码字节[],您需要知道使用了什么编码来确保它能正确解码。

#2


12  

Here's one way to convert an array of bytes into a String and back:

这是将字节数组转换为String并返回的一种方法:

String doc=new String(bytes, "ISO-8859-1");
byte[] bytes2=doc.getBytes("ISO-8859-1");

A String is a sequence of characters, so you'll have to somehow encode bytes as characters. The ISO-8859-1 encoding maps a single, unique character for each byte, so it's safe to use it for the conversion. Note that other encodings, such as UTF-8, are not safe in this sense because there are sequences of bytes that don't map to valid strings in those encodings.

String是一个字符序列,因此您必须以某种方式将字节编码为字符。 ISO-8859-1编码为每个字节映射一个唯一的字符,因此可以安全地将其用于转换。请注意,其他编码(如UTF-8)在这种意义上并不安全,因为有些字节序列不会映射到这些编码中的有效字符串。

#3


1  

The "proper conversion" between byte[] and String is to explicitly state the encoding you want to use. If you start with a byte[] and it does not in fact contain text data, there is no "proper conversion". Strings are for text, byte[] is for binary data, and the only really sensible thing to do is to avoid converting between them unless you absolutely have to.

byte []和String之间的“正确转换”是明确说明要使用的编码。如果以byte []开头并且实际上不包含文本数据,则没有“正确的转换”。字符串用于文本,byte []用于二进制数据,唯一真正明智的做法是避免在它们之间进行转换,除非你绝对不得不这样做。

If you really must use a String to hold binary data then the safest way is to use Base64 encoding.

如果你真的必须使用String来保存二进制数据,那么最安全的方法是使用Base64编码。

Source by Michael Borgwardt

来自Michael Borgwardt的消息来源

#1


16  

You need to specify the encoding you want e.g. for UTF-8

您需要指定所需的编码,例如对于UTF-8

String doc = ....
byte[] bytes = doc.getBytes("UTF-8");
String doc2 = new String(bytes, "UTF-8");

doc and doc2 will be the same.

doc和doc2将是相同的。

To decode a byte[] you need to know what encoding was used to be sure it will decode correctly.

要解码字节[],您需要知道使用了什么编码来确保它能正确解码。

#2


12  

Here's one way to convert an array of bytes into a String and back:

这是将字节数组转换为String并返回的一种方法:

String doc=new String(bytes, "ISO-8859-1");
byte[] bytes2=doc.getBytes("ISO-8859-1");

A String is a sequence of characters, so you'll have to somehow encode bytes as characters. The ISO-8859-1 encoding maps a single, unique character for each byte, so it's safe to use it for the conversion. Note that other encodings, such as UTF-8, are not safe in this sense because there are sequences of bytes that don't map to valid strings in those encodings.

String是一个字符序列,因此您必须以某种方式将字节编码为字符。 ISO-8859-1编码为每个字节映射一个唯一的字符,因此可以安全地将其用于转换。请注意,其他编码(如UTF-8)在这种意义上并不安全,因为有些字节序列不会映射到这些编码中的有效字符串。

#3


1  

The "proper conversion" between byte[] and String is to explicitly state the encoding you want to use. If you start with a byte[] and it does not in fact contain text data, there is no "proper conversion". Strings are for text, byte[] is for binary data, and the only really sensible thing to do is to avoid converting between them unless you absolutely have to.

byte []和String之间的“正确转换”是明确说明要使用的编码。如果以byte []开头并且实际上不包含文本数据,则没有“正确的转换”。字符串用于文本,byte []用于二进制数据,唯一真正明智的做法是避免在它们之间进行转换,除非你绝对不得不这样做。

If you really must use a String to hold binary data then the safest way is to use Base64 encoding.

如果你真的必须使用String来保存二进制数据,那么最安全的方法是使用Base64编码。

Source by Michael Borgwardt

来自Michael Borgwardt的消息来源