Open In App

String.getByte() Method in Java

Improve
Improve
Like Article
Like
Save
Share
Report

In Java, getBytes() encodes a string into a sequence of bytes using the named character set and storing the result into a new byte array. This function can be implemented in two ways. Both ways are discussed below as follows:

  • getBytes()
  • getBytes(Charset charset)

Let us discuss and implement the first use case which is as follows:

Java String getBytes()

This function takes no arguments and used the default charset to encode the string into bytes. getbytes() function in Java is used to convert a string into a sequence of bytes and returns an array of bytes. 

Syntax

public byte[] getBytes()

Example 1:

Java




// Java Program to Demonstrate
// Working of getByte() Method
 
// Importing required classes
import java.util.*;
 
// Main class
// GetByte
public class GFG {
    // Main driver method
    public static void main(String args[])
    {
        // Declaring and initializing a string
        String gfg = "Geeks for Geeks";
 
        // Displaying string values before conversion
        System.out.println(
            "The String before conversion is : ");
        System.out.println(gfg);
 
        // Converting the string into byte
        // using getBytes() method
        byte[] b = gfg.getBytes();
 
        // Display message only
        System.out.println(
            "The String after conversion is : ");
 
        // Printing converted string after conversion
        for (int i = 0; i < b.length; i++) {
            System.out.print(b[i]);
        }
    }
}


Output

The String before conversion is : 
Geeks for Geeks
The String after conversion is : 
71101101107115321021111143271101101107115

Java String getBytes(Charset charset)

Now let us implement and accept the charset according to which string has to be encoded while conversion into bytes. There are many charset defined and are discussed below. 

Syntax

public byte[] getBytes(Charset charset);
  • US-ASCII: Seven-bit ASCII, a.k.a. ISO646-US, a.k.a. the Basic Latin block of the Unicode character set
  • ISO-8859-1: ISO Latin Alphabet No. 1, a.k.a. ISO-LATIN-1
  • UTF-8: Eight-bit UCS Transformation Format
  • UTF-16BE: Sixteen-bit UCS Transformation Format, big-endian byte order
  • UTF-16LE: Sixteen-bit UCS Transformation Format, little-endian byte order
  • UTF-16: Sixteen-bit UCS Transformation Format, byte order identified by an optional byte-order mark.

Example

Java




// Java code to Illustrate Working of getByte() Method
// using Different Character Sets
 
// Importing required classes
import java.io.*;
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String args[])
    {
 
        // Declaring and initializinga string
        String gfg = new String("Geeks for Geeks");
 
        // Displaying string values before conversion
        System.out.println(
            "The String before conversion is : ");
        System.out.println(gfg);
 
        // Try block to check for exceptions
        try {
 
            // Converting the string into byte
            // using getBytes() method
            byte[] b = gfg.getBytes("UTF-16");
 
            // Displaying converted string
            // after conversion into UTF-16
            System.out.println(
                "The String after conversion into UTF-16 is : ");
 
            // Iterating over the string
            for (int i = 0; i < b.length; i++) {
                System.out.print(b[i]);
            }
 
            System.out.print("\n");
 
            // Converting the above string into byte
            // using getBytes() method
            byte[] c = gfg.getBytes("UTF-16BE");
 
            // Displaying converted string
            // after conversion into UTF-16BE
            System.out.println(
                "The String after conversion into UTF-16BE is : ");
 
            for (int i = 0; i < c.length; i++) {
                System.out.print(c[i]);
            }
        }
 
        // Catch block to handle exceptions
        catch (UnsupportedEncodingException g) {
 
            // Display message when exceptions occurs
            System.out.println("Unsupported character set"
                               + g);
        }
    }
}


Output

The String before conversion is : 
Geeks for Geeks
The String after conversion into UTF-16 is : 
-2-107101010101010701150320102011101140320710101010101070115
The String after conversion into UTF-16BE is : 
07101010101010701150320102011101140320710101010101070115


Last Updated : 04 Jun, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads