3

How to convert an array of bytes to a String in Java

 2 years ago
source link: https://marco.dev/java-convert-byte-to-string
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

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:

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 Charsetis not specified Charset.defaultCharset() is used by the constructor.

An example of a simple implementation:

// we build an array of bytes from a String 
byte[] myByteArray = "marco.dev blog".getBytes(); 
 
// now we build a String using the array 
String myNewString = new String(myByteArray); 
 
System.out.println(myNewString) // -> marco.dev blog 

You can specify a Charset:

// 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.

String myString = "marco.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.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK