Java Double to Byte Array doubleToBytes(final double val)

Here you can find the source of doubleToBytes(final double val)

Description

Encodes the double value as an 8 byte array.

License

Open Source License

Parameter

Parameter Description
val The double value to encode.

Return

A byte array of length 8.

Declaration

public static byte[] doubleToBytes(final double val) 

Method Source Code

//package com.java2s;
/**//from w w  w.j a  va2s  . c  om
 * Copyright (c) 2010 Yahoo! Inc., 2016 YCSB contributors. All rights reserved.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License"); you
 * may not use this file except in compliance with the License. You
 * may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * permissions and limitations under the License. See accompanying
 * LICENSE file.
 */

public class Main {
    /**
     * Encodes the double value as an 8 byte array.
     * @param val The double value to encode.
     * @return A byte array of length 8.
     */
    public static byte[] doubleToBytes(final double val) {
        return longToBytes(Double.doubleToRawLongBits(val));
    }

    /**
     * Writes a big-endian 8-byte long at an offset in the given array.
     * @param val The value to encode.
     * @throws IndexOutOfBoundsException if the byte array is too small.
     */
    public static byte[] longToBytes(final long val) {
        final byte[] bytes = new byte[8];
        bytes[0] = (byte) (val >>> 56);
        bytes[1] = (byte) (val >>> 48);
        bytes[2] = (byte) (val >>> 40);
        bytes[3] = (byte) (val >>> 32);
        bytes[4] = (byte) (val >>> 24);
        bytes[5] = (byte) (val >>> 16);
        bytes[6] = (byte) (val >>> 8);
        bytes[7] = (byte) (val >>> 0);
        return bytes;
    }
}

Related

  1. doubleToBytes(double dnum, byte[] bytes, int startIndex)
  2. doubleToBytes(double v, byte[] bytes)
  3. doubleToBytes(double v, byte[] bytes, int off)
  4. doubleToBytes(double value)
  5. doubleToBytes(final double d)
  6. DoubleToBytes_With_Little_Endian(double number)
  7. putDouble(byte[] b, double val)
  8. putDouble(byte[] data, int i, boolean le, double v)
  9. putDouble(double x)