Open In App

Convert a String to a ByteBuffer in Java

Last Updated : 31 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In Java, ByteBuffer can be used to perform operations at the Byte level one more thing is this class provides different types of methods for reading writing, and manipulating bytes in a structured way only. In this article, we will learn about String to ByteBuffer in Java.

Java Program to Convert String to ByteBuffer

Convert a String to a ByteBuffer in Java for this we have used the Charset class which is used to convert a set of characters from a String into a sequence of Bytes in a ByteBuffer. In this article, we have used UTF-8 character encoding format you can use other formats also. Now we give some important key points about the ByteBuffer class to understand quickly.

Steps for Convert a String to a ByteBuffer

  1. First, create one String value or take a String value you want which is the input String value.
  2. After that select a character encoding here, we used UTF-8 which is the charset.
  3. Now convert the String to ByteBuffer by using the encode method in the Charset class.
  4. After that print the original string and ByteBuffer content.

Example 1:

In this example, we take one String value which is Welcome to GeeksForGeeks, and convert it into ByteBuffer content by using the Charset encode method.

Java




// Java Program to Convert String to ByteBuffer
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
  
// Driver Class
public class StringToByteBufferExampleOne {
    // Main function
    public static void main(String[] args) {
          
        // Step 1: Create a String
        String inputString = "Welcome to GeeksForGeeks";
  
        // Step 2: Choose a character encoding (e.g., UTF-8)
        Charset charset = Charset.forName("UTF-8");
  
        // Step 3: Convert the String to a ByteBuffer
        ByteBuffer byteBuffer = charset.encode(inputString);
  
        // Step 4: Print the original String and the ByteBuffer content
        System.out.println("Original String: " + inputString);
  
        // Convert ByteBuffer to byte array for printing
        byte[] byteArray = new byte[byteBuffer.remaining()];
        byteBuffer.get(byteArray);
  
        System.out.print("ByteBuffer Content: ");
        for (byte b : byteArray) {
            System.out.print((char) b);
        }
          
    }
}


Output

Original String: Welcome to GeeksForGeeks
ByteBuffer Content: Welcome to GeeksForGeeks

Explanation of the above Program:

  • In this above code, first we import the ByteBuffer and Charset from the java.nio package in java, after that we take one String value after that we use character encoding format that is UTF-8 by using Charset.forName() method in Charset class.
  • After that we created one object for ByteBuffer which is byteBuffer for this object assign the result of charset.encode method result.
  • Next we print the original String value after that Print the ByteBuffer content.
  • For display ByteBuffer content we take one byte type of array because the ByteBuffer contains set of character bytes that why we use this array.

Example 2:

In example 1 we print the original string value as well as ByteBuffer content only, but in this example, we print every byte in the buffer, but it is in the form of decimal only. And same steps follow what we follow in Example 1.

Java




// Java Program to Convert String to ByteBuffer
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
  
// Driver Class
public class StringToByteBufferExampleTwo {
    // main function
    public static void main(String[] args) {
          
        // Step 1: Create a String
        String inputString = "Welcome to GeeksForGeeks";
  
        // Step 2: Choose a character encoding (e.g., UTF-8)
        Charset charset = Charset.forName("UTF-8");
  
        // Step 3: Convert the String to a ByteBuffer
        ByteBuffer byteBuffer = charset.encode(inputString);
  
        // Step 4: Print the original String and the ByteBuffer content
        System.out.println("\n\tOriginal String: " + inputString);
  
        // Convert ByteBuffer to byte array for printing
        byte[] byteArray = new byte[byteBuffer.remaining()];
        byteBuffer.get(byteArray);
          
        System.out.print("\n\tByteBuffer Content (Decimal Values): ");
        for (byte b : byteArray) {
            System.out.print(b + " ");
        }
  
        System.out.print("\n\n\tByteBuffer Content: ");
        for (byte b : byteArray) {
            System.out.print((char) b);
        }
          
    }
}


Output

    Original String: Welcome to GeeksForGeeks

    ByteBuffer Content (Decimal Values): 87 101 108 99 111 109 101 32 116 111 32 71 101 101 107 115 70 111 114 71 101 101 107 115 

    ByteBuffer Content: Welco...



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads