[ACCEPTED]-Converting byte array to String (Java)-bytearray

Accepted answer
Score: 141

The byte array contains characters in a 12 special encoding (that you should know). The 11 way to convert it to a String is:

String decoded = new String(bytes, "UTF-8");  // example for one encoding type

By The 10 Way - the raw bytes appear may appear as 9 negative decimals just because the java 8 datatype byte is signed, it covers the range 7 from -128 to 127.


-109 = 0x93: Control Code "Set Transmit State"

The value (-109) is a non-printable 6 control character in UNICODE. So UTF-8 is 5 not the correct encoding for that character 4 stream.

0x93 in "Windows-1252" is the "smart 3 quote" that you're looking for, so the Java 2 name of that encoding is "Cp1252". The next 1 line provides a test code:

System.out.println(new String(new byte[]{-109}, "Cp1252")); 
Score: 25

Java 7 and above

You can also pass your desired encoding 4 to the String constructor as a Charset constant from 3 StandardCharsets. This may be safer than passing the encoding 2 as a String, as suggested in the other answers.

For 1 example, for UTF-8 encoding

String bytesAsString = new String(bytes, StandardCharsets.UTF_8);
Score: 11

You can try this.

String s = new String(bytearray);

0

Score: 5
public class Main {

    /**
     * Example method for converting a byte to a String.
     */
    public void convertByteToString() {

        byte b = 65;

        //Using the static toString method of the Byte class
        System.out.println(Byte.toString(b));

        //Using simple concatenation with an empty String
        System.out.println(b + "");

        //Creating a byte array and passing it to the String constructor
        System.out.println(new String(new byte[] {b}));

    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        new Main().convertByteToString();
    }
}

Output

65
65
A

0

Score: 5
public static String readFile(String fn)   throws IOException 
{
    File f = new File(fn);

    byte[] buffer = new byte[(int)f.length()];
    FileInputStream is = new FileInputStream(fn);
    is.read(buffer);
    is.close();

    return  new String(buffer, "UTF-8"); // use desired encoding
}

0

Score: 4

I suggest Arrays.toString(byte_array);

It depends on your purpose. For 8 example, I wanted to save a byte array exactly 7 like the format you can see at time of debug 6 that is something like this : [1, 2, 3] If you want 5 to save exactly same value without converting 4 the bytes to character format, Arrays.toString (byte_array) does this,. But 3 if you want to save characters instead of 2 bytes, you should use String s = new String(byte_array). In this case, s is 1 equal to equivalent of [1, 2, 3] in format of character.

Score: 3

The previous answer from Andreas_D is good. I'm 11 just going to add that wherever you are 10 displaying the output there will be a font 9 and a character encoding and it may not 8 support some characters.

To work out whether 7 it is Java or your display that is a problem, do 6 this:

    for(int i=0;i<str.length();i++) {
        char ch = str.charAt(i);
        System.out.println(i+" : "+ch+" "+Integer.toHexString(ch)+((ch=='\ufffd') ? " Unknown character" : ""));
    }

Java will have mapped any characters 5 it cannot understand to 0xfffd the official 4 character for unknown characters. If you 3 see a '?' in the output, but it is not mapped 2 to 0xfffd, it is your display font or encoding 1 that is the problem, not Java.

More Related questions