tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Articles > Java > Basics > How to convert Byte Array to Double

How to convert Byte Array to Double 

The following example shows converting a Byte array to Double. First create a ByteBuffer with given byte array and then read next 8 bytes from it as a Double.

java.nio.ByteBuffer
  • public static ByteBuffer wrap(byte[] array)
    Wraps a byte array into a buffer.
  • public double getDouble()
    Reads the next eight bytes in the ByteBuffer as a Double.

File Name  :  
com/bethecoder/articles/basics/prim2bytea/ByteArray2DoubleTest.java 
Author  :  Sudhakar KV
Email  :  [email protected]
   
package com.bethecoder.articles.basics.prim2bytea;

import java.nio.ByteBuffer;

public class ByteArray2DoubleTest {

  /**
   @param args
   */
  public static void main(String[] args) {

    //byte [] bytes = ByteBuffer.allocate(8).putDouble(1729.1729).array();
    byte [] bytes = 64, -1014, -7912, -78, -107, -22 };
    System.out.println(ByteBuffer.wrap(bytes).getDouble());
  }

}
   

It gives the following output,
1729.1729



 
  


  
bl  br