/** * @author lautturi.com * Java example: string to byte array / byte array to string in java */ import java.nio.charset.StandardCharsets; import java.util.*; public class Lautturi { public static void main(String[] args) { String str = "hello java"; byte[] bytes = str.getBytes(); String s = new String(bytes, StandardCharsets.UTF_8); System.out.println(Arrays.toString(bytes)); System.out.println(s); } }
output:
[104, 101, 108, 108, 111, 32, 106, 97, 118, 97] hello java