java 如何最好地将 byte[] 数组转换为字符串缓冲区

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5823290/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 12:57:36  来源:igfitidea点击:

How best to convert a byte[] array to a string buffer

java

提问by Guy Rich

I have a number of byte[] array variables I need to convert to string buffers.

我有许多需要转换为字符串缓冲区的 byte[] 数组变量。

is there a method for this type of conversion ?

有这种类型的转换方法吗?

Thanks

谢谢

Thank you all for your responses..However I didn't make myself clear.... I'm using some byte[] arrays pre-defined as public static "under" the class declaration for my java program. these "fields" are reused during the "life" of the process. As the program issues status messages, (written to a file) I've defined a string buffer (mesg_data) that used to format a status message. So as the program executes I tried msg2 = String(byte_array2) I get a compiler error: cannot find symbol symbol : method String(byte[]) location: class APPC_LU62.java.LU62XnsCvr convrsID = String(conversation_ID) ;

谢谢大家的回复..但是我没有说清楚..我正在使用一些字节 [] 数组,这些数组在我的 Java 程序的类声明下预定义为公共静态“下”。这些“字段”在过程的“生命周期”中被重复使用。当程序发出状态消息时,(写入文件)我定义了一个用于格式化状态消息的字符串缓冲区 (mesg_data)。所以当程序执行我试过 msg2 = String(byte_array2) 我得到一个编译器错误:找不到符号符号:方法字符串(字节[])位置:类APPC_LU62.java.LU62XnsCvr convrsID = String(conversation_ID);

example:

例子:

public class LU62XnsCvr extends Object
           .
           .
static String convrsID ; 
static byte[] conversation_ID = new byte[8] ;

So I can't use a "dynamic" define of a string variable because the same variable is used in multiple occurances.

所以我不能使用字符串变量的“动态”定义,因为同一个变量被多次使用。

I hope I made myself clear Thanks ever so much

我希望我说清楚了 非常感谢

Guy

盖伊

回答by helpermethod

String s = new String(myByteArray, "UTF-8");
StringBuilder sb = new StringBuilder(s);

回答by Itay Maman

There is a constructor that a byte array and encoding:

有一个字节数组和编码的构造函数:

 byte[] bytes = new byte[200];
 //...

 String s = new String(bytes, "UTF-8");

In order to translate bytes to characters you need to specify encoding: the scheme by which sequences (typically of length 1,2 or 3) of 0-255 values (that is: sequence of bytes) are mapped to characters. UTF-8 is probably the best bet as a default.

为了将字节转换为字符,您需要指定编码:0-255 个值(即:字节序列)的序列(通常长度为 1,2 或 3)映射到字符的方案。UTF-8 可能是默认的最佳选择。

回答by Martin

You can turn it to a Stringdirectly

你可以String直接把它变成

byte[] bytearray
....
String mystring = new String(bytearray)

and then to convert to a StringBuffer

然后转换为 StringBuffer

StringBuffer buffer = new StringBuffer(mystring)

回答by Void

You may use

您可以使用

str = new String(bytes)

By thewhat the code above does is to create a java String (i.e. UTF-16) with the default platform character encoding.

通过上面的代码所做的是创建一个带有默认平台字符编码的java字符串(即UTF-16)。

If the byte array was created from a string encoded in the platform default character encoding this will work well.

如果字节数组是从以平台默认字符编码中编码的字符串创建的,这将很好地工作。

If not you need to specify the correct character encoding (Charset) as

如果不是,您需要将正确的字符编码(Charset)指定为

String str = new String (byte [] bytes, Charset charset)

回答by pickypg

It depends entirely on the character encoding, but you want:

这完全取决于字符编码,但您想要:

String value = new String(bytes, "US-ASCII");

This would work for US-ASCII values.

这适用于 US-ASCII 值。

See Charsetfor other valid character encodings (e.g., UTF-8)

有关Charset其他有效字符编码(例如,UTF-8)的信息,请参阅