Saved Bookmarks
| 1. |
How can a Java string be converted into a byte array? |
|
Answer» The getBytes() METHOD allows you to convert a string to a BYTE array by encoding or converting the specified string into a sequence of bytes using the default charset of the PLATFORM. Below is a Java program to convert a Java String to a byte array. Example: import java.util.Arrays;public CLASS StringToByteArray { public static void main(String[] args) { String STR = "Scaler"; byte[] byteArray = str.getBytes(); // print the byte[] elements System.out.println("String to byte array: " + Arrays.toString(byteArray)); }}Output: String to byte array: [83, 99, 97, 108, 101, 114] |
|