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

How to convert Double to Byte Array 

The following example shows converting a Double to Byte array. Since a Java Double occupies 8 bytes allocate a ByteBuffer of capacity 8 and write the Double to ByteBuffer. It updates the ByteBuffer with 8 bytes representing the Double. Finally convert the ByteBuffer to byte array.

  • public static ByteBuffer allocate(int capacity)
    Allocates a new byte buffer of given capacity.
  • public ByteBuffer putDouble(double value)
    Writes eight bytes containing the given double value to this ByteBuffer.
  • public final byte[] array()
    Returns the ByteBuffer as byte array.

File Name  :  
Author  :  Sudhakar KV
Email  :
package com.bethecoder.articles.basics.prim2bytea;

import java.nio.ByteBuffer;
import java.util.Arrays;

public class Double2ByteArrayTest {

   @param args
  public static void main(String[] args) {
    //Since double occupies 8 bytes allocate a buffer of size 8
    byte [] bytes = ByteBuffer.allocate(8).putDouble(1729.1729).array();


It gives the following output,
[64, -101, 4, -79, 12, -78, -107, -22]



Books Recommendation

bl  br