Java String getBytes():
The getBytes() method of a String class in Java encodes a string into a sequence of bytes and stores that sequence of bytes in a bytes array.
Method signature:
The signature for a getBytes() method is as shown below.
public byte[] getBytes()
Method parameters and return type:
Return type: It returns a sequence of bytes.
Parameters: It doesn’t take any parameters.
Throws: It throws UnsupportedEncodingException if the given charset is not supported.
Example 1:
In this example, we have converted the string characters into bytes and stored those bytes into an array using the getBytes() method. After that, we have simply printed the values of the array of bytes.
Source code:
public class Example1 {
public static void main(String[] args) {
String str1 = "Random String";
byte[] bytes = str1.getBytes();
for (int i = 0; i < bytes.length; i++) {
System.out.println("Byte of a character at index"+i+" "+bytes[i]);
}
}
}
Output:
run:
Byte of a character at index0 82
Byte of a character at index1 97
Byte of a character at index2 110
Byte of a character at index3 100
Byte of a character at index4 111
Byte of a character at index5 109
Byte of a character at index6 32
Byte of a character at index7 83
Byte of a character at index8 116
Byte of a character at index9 114
Byte of a character at index10 105
Byte of a character at index11 110
Byte of a character at index12 103
BUILD SUCCESSFUL (total time: 0 seconds)
Example 2:
You can also convert the array of bytes back to a string. Here is an example explaining this concept of conversion.
Source code:
public class Example2 {
public static void main(String[] args) {
String str1 = "Random String";
byte[] bytes = str1.getBytes();
for (int i = 0; i < bytes.length; i++) {
System.out.println("Byte of a character at index" + i + " " + bytes[i]);
}
//converting back to string
String str2 = new String(bytes);
System.out.println("Bytes converted back to a string: "+str2);
}
}
Output:
run:
Byte of a character at index0 82
Byte of a character at index1 97
Byte of a character at index2 110
Byte of a character at index3 100
Byte of a character at index4 111
Byte of a character at index5 109
Byte of a character at index6 32
Byte of a character at index7 83
Byte of a character at index8 116
Byte of a character at index9 114
Byte of a character at index10 105
Byte of a character at index11 110
Byte of a character at index12 103
Bytes converted back to a string: Random String
BUILD SUCCESSFUL (total time: 0 seconds)
Example 3:
In this example, we have passed a charset as a parameter in the method.
Source code:
public class Example3 {
public static void main(String[] args) {
String s = "This is a Java tutorial";
System.out.print("Input string : ");
System.out.println(s);
byte[] array = s.getBytes("UTF-16LE");
System.out.println("After conversion : ");
for (int i = 0; i < array.length; i++) {
System.out.print(array[i]);
}
}
}
Output:
run:
Input string : This is a Java tutorial
After conversion :
8401040105011503201050115032097032074097011809703201160117011601110114010509701080
BUILD SUCCESSFUL (total time: 0 seconds)
References
Happy Learning 🙂