How to convert Strings to and from UTF8 byte arrays in Java
Convert from String to byte[]:
String s = "some text here";byte[] b = s.getBytes(StandardCharsets.UTF_8);
Convert from byte[] to String:
byte[] b = {(byte) 99, (byte)97, (byte)116};String s = new String(b, StandardCharsets.US_ASCII);
You should, of course, use the correct encoding name. My examples used US-ASCII and UTF-8, the two most common encodings.
Here's a solution that avoids performing the Charset lookup for every conversion:
import java.nio.charset.Charset;private final Charset UTF8_CHARSET = Charset.forName("UTF-8");String decodeUTF8(byte[] bytes) { return new String(bytes, UTF8_CHARSET);}byte[] encodeUTF8(String string) { return string.getBytes(UTF8_CHARSET);}
String original = "hello world";byte[] utf8Bytes = original.getBytes("UTF-8");