What is the best way to convert a byte [] array to a string buffer

I have a few byte array variables that I need to convert to string buffers.

Is there a conversion method of this type?

thank

Thank you all for your answers ... But I myself did not understand. I use some byte [] arrays, predefined as public static "under" the class declaration for my java program. these "fields" are reused during the "life" of the process. Since the program displays status messages (written to a file), I defined a string buffer (mesg_data), which is used to format the status message. Since the program is running I tried msg2 = String (byte_array2) I get a compiler error: I can not find the symbol symbol: String method (byte []) location: class APPC_LU62.java.LU62XnsCvr convrsID = String (convers_ID);

example:

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

Therefore, I cannot use the "dynamic" definition of a string variable, because the same variable is used in several cases.

,

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

, :

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

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

, : , ( 1,2 3) 0-255 ( : ) . UTF-8, , .

+5

String

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

StringBuffer

StringBuffer buffer = new StringBuffer(mystring)
+3

str = new String(bytes)

, , java (, UTF-16) .

, , .

(Charset)

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

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

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

This will work for US-ASCII values.

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

0
source

All Articles