![]() |
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 ByteBufferConvert 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
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
Output
Original String: Welcome to GeeksForGeeks ByteBuffer Content: Welcome to GeeksForGeeks Explanation of the above Program:
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
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... |
Reffered: https://www.geeksforgeeks.org
Java Programs |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 13 |