Java Double to Byte Array doubleToBytes(double dnum, byte[] bytes, int startIndex)

Here you can find the source of doubleToBytes(double dnum, byte[] bytes, int startIndex)

Description

Given a double, convert it into a byte array

License

Open Source License

Parameter

Parameter Description
dnum the double given to convert
bytes the bytes where to store the result
startIndex the starting index of the array where the result is stored.

Declaration

public static int doubleToBytes(double dnum, byte[] bytes, int startIndex) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**//from w w  w  .  j a  va  2 s  .  c  o  m
     * Given a double, convert it into a byte array
     * 
     * @param dnum
     *            the double given to convert
     * @param bytes
     *            the bytes where to store the result
     * @param startIndex
     *            the starting index of the array where the result is stored.
     * @ret the index of the array after storing this double
     */
    public static int doubleToBytes(double dnum, byte[] bytes, int startIndex) {
        return longToBytes(Double.doubleToLongBits(dnum), bytes, startIndex);
    }

    /**
     * Given a long, convert it into a byte array
     * 
     * @param lnum
     *            the long given to convert
     * @param bytes
     *            the bytes where to store the result
     * @param startIndex
     *            the starting index of the array where the result is stored.
     * @ret the index of the array after storing this long
     */
    public static int longToBytes(long lnum, byte[] bytes, int startIndex) {
        for (int i = 0; i < 8; i++)
            bytes[startIndex + i] = (byte) ((lnum >> (i * 8)) & 0xff);
        return startIndex + 8;
    }
}

Related

  1. doubleToBytes(double d)
  2. doubleToBytes(double d)
  3. doubleToBytes(double d)
  4. doubleToBytes(double d, byte[] bytes, int off, boolean bigEndian)
  5. doubleToBytes(double d, byte[] data, int[] offset)
  6. doubleToBytes(double v, byte[] bytes)
  7. doubleToBytes(double v, byte[] bytes, int off)
  8. doubleToBytes(double value)
  9. doubleToBytes(final double d)