We can use String getBytes() method to convert String to byte array and we can use String constructor new String(byte[] arr) to convert byte array to String.
Here is a simple program showing how to convert String to byte array in java.
import java.util.Arrays;
public class StringToByteArray {
public static void main(String[] args) {
String str = "PANKAJ";
byte[] byteArr = str.getBytes();
// print the byte[] elements
System.out.println("String to byte array: " + Arrays.toString(byteArr));
}
}
Output:
String to byte array: [80, 65, 78, 75, 65, 74]