Wednesday, December 11, 2019

How To Convert Bytebuffer To String Inward Java

You tin easily convert ByteBuffer to String  in Java if you lot know how to convert byte array to String. Why? because it's really slowly to convert ByteBuffer to a byte array together with vice versa. All you lot take away to practise is telephone band the ByteBuffer.array() method, it volition render you lot the byte array used past times java.nio.ByteBuffer class, afterward you lot tin easily create String from that byte array. Though e'er recollect to render right grapheme encoding piece converting byte array to String. For example, if you lot know that ByteBuffer is filled amongst bytes encoded inwards UTF-8 hence you lot must utilisation the same encoding piece creating String from that byte array. String flat provides an overloaded constructor which accepts grapheme encoding along amongst byte array.  You tin utilisation the snippet shared inwards this representative to practise the job. ByteBuffer is i of the really useful flat inwards java.nio packet which is used to read information from channels together with write information into channel directly.

Same ByteBuffer tin live on used to read together with write data. If you lot desire to read from ByteBuffer only telephone band the flip() method together with it volition convert ByteBuffer into reading mode. In this article, you lot volition larn how to convert ByteBuffer to String inwards Java. I guide maintain a given a unproblematic example, but if you lot nonetheless whatsoever dubiety you lot tin e'er move 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 guide maintain outset created a String literal, hence I guide maintain got the byte array from that String inwards UTF-8 encoding scheme.



After that, I guide maintain created a ByteBuffer object past times using same byte array together with using ByteBuffer.wrap() method. This is just the same province of affairs when your ByteBuffer is filled amongst information whether you lot read it from the network or a RandomAccessFile inwards Java. I guide maintain only used this direct approach to practise a ByteBuffer amongst information to simplify this example.

Next piece of work of this plan is most of import because in that place nosotros are converting ByteBuffer to String. In companionship to acquire the byte array from ByteBuffer only telephone band the ByteBuffer.array() method. This method volition render the backed array.

 Now you lot tin telephone band the String constructor which accepts a byte array together with grapheme encoding to practise String. You are done.

hither is our consummate code representative 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 like to how you lot convert  * byte array to String.   *   * @author Javin Paul  */ public class HelloHP {      public static void main(String args[]) throws UnsupportedEncodingException {                 //Let's outset practise 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 well-nigh how to convert ByteBuffer to String inwards Java. Just recollect to utilisation the right grapheme encoding because without using proper grapheme encoding in that place is no guarantee that you lot volition acquire master string back. Though UTF-8 is a proficient default it cannot live on used to render guaranteed.

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

Further Learning
Data Structures together with Algorithms: Deep Dive Using 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 release to decimal in Java? (tutorial)
  • How to convert Array to Set together with 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 together with 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 missive of the alphabet inwards Java? (example)
  • No comments:

    Post a Comment