How to convert Strings to and from UTF8 byte arrays in Java

To convert a String to a UTF-8 encoded byte array in Java, you can use the getBytes method of the String class and specify the character encoding as "UTF-8". Here's an example:

String str = "Hello, world!";
byte[] utf8Bytes = str.getBytes("UTF-8");

To convert a UTF-8 encoded byte array to a String in Java, you can use the String constructor that takes a byte array and a character encoding as arguments. Here's an example:

byte[] utf8Bytes = {72, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100, 33};
String str = new String(utf8Bytes, "UTF-8");

You can also use the StandardCharsets class introduced in Java 7 to specify the character encoding. For example:

byte[] utf8Bytes = {72, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100, 33};
String str = new String(utf8Bytes, StandardCharsets.UTF_8);

Keep in mind that the getBytes method and the String constructor can throw an UnsupportedEncodingException if the specified character encoding is not supported. You can either catch this exception or use the StandardCharsets class to avoid the need to catch the exception.