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
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
public class StringToByteBufferExampleOne {
public static void main(String[] args) {
String inputString = "Welcome to GeeksForGeeks" ;
Charset charset = Charset.forName( "UTF-8" );
ByteBuffer byteBuffer = charset.encode(inputString);
System.out.println( "Original String: " + inputString);
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
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
public class StringToByteBufferExampleTwo {
public static void main(String[] args) {
String inputString = "Welcome to GeeksForGeeks" ;
Charset charset = Charset.forName( "UTF-8" );
ByteBuffer byteBuffer = charset.encode(inputString);
System.out.println( "\n\tOriginal String: " + inputString);
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...
Share your thoughts in the comments
Please Login to comment...