How to convert an array of bytes to a String in Java
How to convert byte[] to String and vice versa
Pretty funny to convert an array of bytes to a String or vice versa. String stores his value in a byte[], so the conversion should be straightforward.
Convert byte[] to String
In String.java we have a lot of constructors that accept a byte array:
``` java String(byte[] bytes, int offset, int length, String charsetName) throws UnsupportedEncodingException String(byte[] bytes, String charsetName) throws UnsupportedEncodingException String(byte[] bytes, java.nio.charset.Charset charset) String(byte[] bytes, int offset, int length) String(byte[] bytes)
If the `Charset`is not specified `Charset.defaultCharset()` is used by the constructor.
An example of a simple implementation:
``` java
// we build an array of bytes from a String
byte[] myByteArray = "marmo.dev blog".getBytes();
// now we build a String using the array
String myNewString = new String(myByteArray);
System.out.println(myNewString) // -> marmo.dev blogYou can specify a Charset:
``` java // Charsets are defined in java.nio.charset.StandardCharsets
String myNewString = new String(myByteArray, StandardCharsets.UTF_16)
## Convert String to byte[]
In the previous example we used `public byte[] getBytes()` present in _String.java_.
This is the recommended method, you can pass a _charsetName_ to encode correctly the array.
``` java
String myString = "marmo.dev blog en français";
byte[] myDefaultArrayOfBytes = myString.getBytes(); // byte[27] { 109, 97, 114, 99, 111, 46, 100, 101, 118, 32, 98, 108, 111, 103, 32, 101, 110, 32, 102, 114, 97, 110, -61, -89, 97, 105, 115 }
byte[] myUTF8ArrayOfBytes = myString.getBytes("ISO-8859-1") // byte[26] { 109, 97, 114, 99, 111, 46, 100, 101, 118, 32, 98, 108, 111, 103, 32, 101, 110, 32, 102, 114, 97, 110, -25, 97, 105, 115 }
In our example you can see how the result differs if I use the default Charset (UTF-8) and the ISO-8859-1.