How to convert ByteBuffer to String inwards Java

You tin easily convert ByteBuffer to String  in Java if yous know how to convert byte array to String. Why? because it's really slowly to convert ByteBuffer to a byte array in addition to vice versa. All yous take away to do is telephone phone the ByteBuffer.array() method, it volition render yous the byte array used past times java.nio.ByteBuffer class, afterwards yous tin easily create String from that byte array. Though e'er squall upward to render right graphic symbol encoding spell converting byte array to String. For example, if yous know that ByteBuffer is filled amongst bytes encoded inwards UTF-8 in addition to thus yous must usage the same encoding spell creating String from that byte array. String cast provides an overloaded constructor which accepts graphic symbol encoding along amongst byte array.  You tin usage the snippet shared inwards this instance to do the job. ByteBuffer is i of the really useful cast inwards java.nio packet which is used to read information from channels in addition to write information into channel directly.

Same ByteBuffer tin hold upward used to read in addition to write data. If yous desire to read from ByteBuffer simply telephone phone the flip() method in addition to it volition convert ByteBuffer into reading mode. In this article, yous volition larn how to convert ByteBuffer to String inwards Java. I possess got a given a unproblematic example, but if yous even thus whatever uncertainty yous tin e'er acquire out a comment or question.


ByteBuffer to String inwards Java - Example

This is i of the simplest examples  to demonstrate how to convert ByteBuffer to String. In this Java program, I possess got showtime created a String literal, in addition to thus I possess got got the byte array from that String inwards UTF-8 encoding scheme.


After that, I possess got created a ByteBuffer object past times using same byte array in addition to using ByteBuffer.wrap() method. This is precisely the same province of affairs when your ByteBuffer is filled amongst information whether yous read it from the network or a RandomAccessFile inwards Java. I possess got simply used this lead approach to do a ByteBuffer amongst information to simplify this example.

Next travel of this programme is most of import because in that place nosotros are converting ByteBuffer to String. In guild to acquire the byte array from ByteBuffer simply telephone phone the ByteBuffer.array() method. This method volition render the backed array.

 Now yous tin telephone phone the String constructor which accepts a byte array in addition to graphic symbol encoding to do String. You are done.

hither is our consummate code instance for converting ByteBuffer to String :


import java.io.UnsupportedEncodingException; import java.nio.ByteBuffer;   /**  * Java Program to convert ByteBuffer to String.  * It's slowly because its similar to how yous convert  * byte array to String.   *   * @author Javin Paul  */ public class HelloHP {      public static void main(String args[]) throws UnsupportedEncodingException {                 //Let's showtime do a ByteBuffer from String         String content = "It's slowly to convert ByteBuffer to String inwards Java";         ByteBuffer buffer = ByteBuffer.wrap(content.getBytes("UTF-8"));                 // Now let's convert this ByteBuffer to String         String converted = new String(buffer.array(), "UTF-8");                  System.out.println("Original : " + content);         System.out.println("Converted : " + converted);      }  }  Output : Original : It's slowly to convert ByteBuffer to String inwards Java Converted : It's slowly to convert ByteBuffer to String inwards Java

That's all almost how to convert ByteBuffer to String inwards Java. Just squall upward to usage the right graphic symbol encoding because without using proper graphic symbol encoding in that place is no guarantee that yous volition acquire master copy string back. Though UTF-8 is a practiced default it cannot hold upward used to render guaranteed.

in Java if yous know how to convert byte array to String How to convert ByteBuffer to String inwards Java


If yous similar this article in addition to desire to larn to a greater extent than almost converting i type to around other inwards Java, yous tin likewise cheque next Java tutorials :
  • How to convert float to String inwards Java? (tutorial)
  • How to convert Double to Long inwards Java? (tutorial)
  • How to convert Character to String inwards Java? (tutorial)
  • How to convert String to Int inwards Java? (example)
  • 5 Ways to convert Java 8 Stream to List? (solution)
  • How to convert SQL appointment to Util Date inwards Java? (example)
  • 5 examples of converting InputStream to String inwards Java? (tutorial)
  • How to convert Enum to String inwards Java? (guide)
  • How to convert a binary publish to decimal in Java? (tutorial)
  • How to convert Array to Set in addition to List inwards Java? (example)
  • 3 examples of converting Array to ArrayList inwards Java (tutorial)
  • How to convert String to Enum inwards Java? (tutorial)
  • How to convert Hexadecimal numbers to octal in addition to binary inwards Java? (tutorial)
  • The best agency to convert Numbers to String inwards Java? (guide)
  • How to convert java.util.Date to java.sql.Date inwards Java? (tutorial)
  • How to convert ArrayList to Set inwards Java? (tutorial)
  • How to convert List to Set inwards Java? (example)
  • How to convert lowercase String to working capital inwards Java? (example)

Subscribe to receive free email updates:

0 Response to "How to convert ByteBuffer to String inwards Java"

Posting Komentar