Java Program to Convert String to Byte Array Using getBytes() Method Last Updated : 19 Jan, 2021 Comments Improve Suggest changes Like Article Like Report In Java, strings are objects that are backed internally by a char array. So to convert a string to a byte array, we need a getByte() method. It is the easiest way to convert a string to a byte array. This method converts the given string to a sequence of bytes using the platform's default charset and returns an array of bytes. It is a predefined function of string class. But it is an error-prone method and can return an erroneous result if the platform character encoding doesn't match with the expected encoding. Syntax: public byte[] getBytes() Note: In this method, the platform's default encoding is used to convert the given string to the byte array if you do not specify any character encoding in the method.The length of the byte array is not the same as the given string, it depends upon the character encoding. Example 1: Java // Java program to illustrate how to // convert a string to byte array // Using getBytes() import java.io.*; class GFG { public static void main(String[] args) { // Initializing String String ss = "Hello GeeksforGeeks"; // Display the string before conversion System.out.println("String: " + ss); // Converting string to byte array // Using getBytes() method byte[] res = ss.getBytes(); System.out.println("Byte Array:"); // Display the string after conversion for (int i = 0; i < res.length; i++) { System.out.print(res[i]); } } } OutputString: Hello GeeksforGeeks Byte Array: 72101108108111327110110110711510211111471101101107115 Example 2: Java // Java program to illustrate how to // convert a string to byte array // Using getBytes() import java.io.*; import java.util.Arrays; class GFG { public static void main(String[] args) { // Initializing String String ss = "GeeksforGeeks"; // Display the string before conversion System.out.println("String: " + ss); // Converting string to byte array // Using getBytes() method byte[] res = ss.getBytes(); // Display the string after conversion System.out.println("Byte Array:" + Arrays.toString(res)); } } OutputString: GeeksforGeeks Byte Array:[71, 101, 101, 107, 115, 102, 111, 114, 71, 101, 101, 107, 115] Comment More infoAdvertise with us Next Article Java Program to Convert String to Byte Array Using getBytes() Method A ankita_saini Follow Improve Article Tags : Java Java Programs Java-String-Programs Practice Tags : Java Similar Reads Convert String to Byte Array in Java Using getBytes(Charset) Method To convert a string to a byte array, we use the getBytes(Charset) method In Java that transforms the string into a sequence of bytes based on a specified character encoding. This method returns the byte array and relies on the Charset class to handle character-to-byte mappings.Example:Java// Java pr 3 min read Convert String to Byte Array in Java Using getBytes(encoding) Method In Java, any sequence of characters within double quotes is treated as String literal. String class represents the character string literal. The string class is present in java.lang package. All string literals in java are immutable i.e their value cannot be changed once created. In Java, the string 3 min read Java Program to Convert Byte Array to Hex String Byte Array - A Java Byte Array is an array used to store byte data types only. The default value of each element of the byte array is 0. Hex String - A Hex String is a combination of the digits 0-9 and characters A-F, just like how a binary string comprises only 0's and 1's. Eg: "245FC" is a hexadec 5 min read Java Program to Convert Hex String to Byte Array Hex String - A Hex String is a combination of the digits 0-9 and characters A-F, just like how a binary string comprises only 0's and 1's. Eg: "245FC" is a hexadecimal string. Byte Array - A Java Byte Array is an array used to store byte data types only. The default value of each element of the byte 4 min read Java Program to Convert Byte Array to Image A byte array is the array of bytes that is used to store the collection of binary data. For example, the byte array of an image stores the information of every pixel of the image. In the byte array, we can store the content of any file in binary format. We can initialize the byte array with the byte 3 min read Like