Convert a String to a ByteBuffer in Java
Last Updated :
31 Jan, 2024
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
- First, create one String value or take a String value you want which is the input String value.
- After that select a character encoding here, we used UTF-8 which is the charset.
- Now convert the String to ByteBuffer by using the encode method in the Charset class.
- 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);
}
}
}
OutputOriginal 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...
Similar Reads
Convert base64 String to ArrayBuffer In JavaScript A Base64 string represents binary data in an ASCII string format by translating it into a radix-64 representation. Often used to encode binary data in text-based formats like JSON or HTML, it needs to be converted back into its original binary format for further processing. An ArrayBuffer in JavaScr
2 min read
Convert byte[] array to File using Java As we know whenever it comes to writing over a file, write() method of the File class comes into play but here we can not use it in order to convert that byte into a file. In order to convert a byte array to a file, we will be using a method named the getBytes() method of String class. Implementatio
3 min read
Program to convert Byte Array to Writer in Java References: Writer Class Approach: Writer class is used to write character stream, by which byte array can be passed as an argument. By this way, byte array can be converted into Writer class. To get the byte array from String, getBytes() method is used. Below is the implementation of the above appr
2 min read
ByteBuffer toString() method in Java with Examples The toString() method of ByteBuffer class is the inbuilt method used to returns a string representing the data contained by ByteBuffer Object. A new String object is created and initialized to get the character sequence from this ByteBuffer object and then String is returned by toString(). Subsequen
2 min read
ByteBuffer array() method in Java with Examples The array() method of java.nio.ByteBuffer class is used to return the byte array that backs the taken buffer.Modifications to this buffer's content will cause the returned array's content to be modified, and vice versa.Invoke the hasArray() method before invoking this method in order to ensure that
3 min read
ByteBuffer putInt() methods in Java with Examples putInt(int value) The putInt(int value) method of java.nio.ByteBuffer Class is used to write four bytes containing the given int value, in the current byte order, into this buffer at the current position, and then increments the position by four. Syntax : public abstract ByteBuffer putInt(int value)
6 min read