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 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 Java Program to Convert Byte Array to Long 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. Given an array of bytes, convert it to a long value. Example: Byte Array: 1 2 3 4 Long Value: 16909060 Equivalent Hexadecimal String: 0x1020304 There are numerous approaches fo 3 min read Java Program to Convert File to a Byte Array Here, we will go through the different ways to convert file to byte array in Java. Note: Keep a check that prior doing anything first. Create a file on the system repository to deal with our program\writing a program as we will be accessing the same directory through our programs. Methods: Using rea 3 min read Java Program to Convert Binary String to Decimal Using Wrapper Class Given a binary string as input, we need to write a program to convert the given binary string into its equivalent decimal number. Examples: Input : 1111 Output : 15 Input : 1001101 Output : 77 Input : 1101 Output : 13 The idea is to extract each character of a given binary string using charAt() and 2 min read Java Program to Convert String to InputStream Given a string, the task is to convert the string to InputStream which is shown in the below illustrations. Illustration: Input : String : "Geeks for Geeks" Output : Input Stream : Geeks for Geeks Input : String : "A computer science portal" Output : Input stream : A computer science portal In order 2 min read Java Program to Convert InputStream to String Read and Write operations are basic functionalities that users perform in any application. Every programming language provides I/O streams to read and write data. The FileInputStream class and FileOutputStream class of Java performs I/O operations on files. The FileInputStream class is used to read 4 min read Like