Java String getBytes()

The Java String.getBytes method is one of the Java String Method which is used to encode the given string into sequence of bytes using the user specified charset, and return Byte array. In this article we will show you, How to write Java String getBytes method with example. Before we get into the example, let us see the basic syntax of the string.getBytes in Java Programming language is as shown below.

Java String getBytes syntax

The Java Programming Language provides three different getBytes methods to encode the string. Following method will not accept any argument and encode the given string into sequence of bytes using the default charset.

public byte[] getBytes(); // It will return Byte Array //In order to use in program String_Object.getBytes();

Following method will accept Charset as an argument and encode the given string into sequence of bytes using the user specified charset. It means, we are allowing the user to specify the Charset (dynamic).

public byte[] getBytes(Charset charset); // It will return Byte Array //In order to use in program String_Object.getBytes(Charset charset);

Following method will accept Charset name as an argument and encode the given string into sequence of bytes by invoking the specified charset name. It means, we are calling required Charset name (static).

public byte[] getBytes(String Charset_Name); // It will return Byte Array //In order to use in program String_Object.getBytes(String Charset_Name);
Internal implementation
public byte[] getBytes() { return StringCoding.encode(value, 0, value.length); }
Return Value

The Java String getBytes Method encodes this string into sequence of bytes using the user specified charset, and it will store the result in a byte array.

Java String getBytes Example

The Java String.getBytes method is used to encode the given string into sequence of bytes and returns byte array. In this Java program, We are going to encode the string using the platform default charset.

package StringFunctions; public class getBytesMethod { public static void main(String[] args) { String str = "ABCDE"; byte[] byteArray = str.getBytes(); System.out.println("Byte Array after using the getBytes Method:"); arrayPrint(byteArray); String str1 = "abcd"; byte[] bitArray = str1.getBytes(); System.out.println("\nByte Array after using the getBytes Method:"); arrayPrint(bitArray); } public static void arrayPrint(byte[] anByteArray) { for (byte Number: anByteArray) { System.out.format("%d \t", Number); } } }


Output:
Byte array after using the getBytes Method; 65 66 67 68 69 Byte Array after using the getBytes Method 97 98 99 100
ANALYSIS

First we declared two String variables str, str1 and assigned corresponding values using following statement

String str = "ABCDE"; String str1 = "abcd";
byte[] byteArray = str.getBytes(); byte[] bitArray = str1.getBytes();

Following statement is used to print the Byte array elements to the output

arrayPrint(byteArray);

When the compiler reaches to the above statement, compiler will jump to following function. From the below code snippet you can observe that, we used the Java Foreach Loop to iterate the Byte Array and then we are printing each and every array element using the System.out.println statement

public static void arrayPrint(byte[] anByteArray) { for (byte Number: anByteArray) { System.out.format("%d \t", Number); } }

Following statements will call the public byte [] getBytes () method to encode the above specified string (str & str1) into sequence of bytes. From the above screenshot you can observe that, both these statements are using the platform default charset.

Java String getBytes() method example
public class Test { public static void main(String args[]){ String s1="ABCDEFGHIJKLMNOPQRSTUVWXYZ"; byte[] barr=s1.getBytes(); for(int i=0;i<barr.length;i++){ System.out.println(barr[i]); } } }


Output:
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
Java String getBytes() Method Example
public class Test { public static void main(String[] args) { String s1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; byte[] barr = s1.getBytes(); for(int i=0;i<barr.length;i++){ System.out.println(barr[i]); } // Getting string back String s2 = new String(barr); System.out.println(s2); } }


Output:
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 ABCDEFGHIJKLMNOPQRSTUVWXYZ
package StringFunctions; import java.io.UnsupportedEncodingException; public class getBytesMethod2 { public static void main(String[] args) { String str = new String("A" + "\u00ea" + "\u00f1" + "\u00fc" + "C"); try { byte[] byteArray = str.getBytes(); System.out.println("Byte Array after using the getBytes Method:"); arrayPrint(byteArray); String s1 = new String(byteArray); System.out.println("\nDecryted Text = " + s1); byte[] bitArray = str.getBytes("UTF-8"); System.out.println("\nByte Array after using the getBytes Method:"); arrayPrint(bitArray); String s2 = new String(bitArray); System.out.println("\nDecryted Text = " + s2); byte[] bArray = str.getBytes("ISO-8859-1"); System.out.println("\nByte Array after using the getBytes Method:"); arrayPrint(bArray); String s3 = new String(bArray); System.out.println("\nDecryted Text = " + s3); } catch( UnsupportedEncodingException e){ System.out.println("Unsupported character set"); } } public static void arrayPrint(byte[] anByteArray) { for (byte bit: anByteArray) { System.out.format("%d \t", bit); } } }


ANALYSIS

First we declared a String variables str and assigned non-Unicode text using following statement

String str = new String("A" + "\u00ea" + "\u00f1" + "\u00fc" + "C");

Following statements will call the public byte [] getBytes () method to encode the above specified string (str) into sequence of bytes. It is using the platform default charset.

byte[] byteArray = str.getBytes();

Following statements will call the public byte [] getBytes (Charset charset) method to encode the above specified string (str) into sequence of bytes using standard charset UTF-8.

byte[] bitArray = str.getBytes("UTF-8");

Following statements will call the public byte [] getBytes (Charset charset) method to encode the above specified string (str) into sequence of bytes using standard charset ISO-8859-1.

byte[] bArray = str.getBytes("ISO-8859-1");

Following statement is used to convert the Byte array to string

String s3 = new String(bArray);



Instagram