Introduction
In this post, we will explore how to convert an array of bytes i.e. byte[] to String in Java. For this functionality, we will use the following constructor of java.lang.String class
String(byte[] bytes, String enc)
Example
import java.nio.charset.StandardCharsets;
public class ByteArrayToString {
public static void main(String[] args) {
byte[] bytes = "a quick fox jumps over the lazy dog".getBytes();
// convert the bytes to String
System.out.println("bytes = " + bytes);
System.out.println( "bytes.toString() = " + bytes.toString());
String myString = new String ( bytes , StandardCharsets.UTF_8);
System.out.println("myString = " + myString);
}
}
Comments
Post a Comment