Java Double to Byte Array doubleToBytes(double d)

Here you can find the source of doubleToBytes(double d)

Description

This function converts a double to its corresponding byte array format.

License

Open Source License

Parameter

Parameter Description
d The double to be converted.

Return

The corresponding byte array.

Declaration

static public byte[] doubleToBytes(double d) 

Method Source Code

//package com.java2s;
/************************************************************************
 * Copyright (c) Crater Dog Technologies(TM).  All Rights Reserved.     *
 ************************************************************************
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.        *
 *                                                                      *
 * This code is free software; you can redistribute it and/or modify it *
 * under the terms of The MIT License (MIT), as published by the Open   *
 * Source Initiative. (See http://opensource.org/licenses/MIT)          *
 ************************************************************************/

public class Main {
    /**//from  ww w.j a  v a2s . com
     * This function converts a double to its corresponding byte array format.
     *
     * @param d The double to be converted.
     * @return The corresponding byte array.
     */
    static public byte[] doubleToBytes(double d) {
        byte[] buffer = new byte[8];
        doubleToBytes(d, buffer, 0);
        return buffer;
    }

    /**
     * This function converts a double into its corresponding byte format and inserts
     * it into the specified buffer.
     *
     * @param d The double to be converted.
     * @param buffer The byte array.
     * @return The number of bytes inserted.
     */
    static public int doubleToBytes(double d, byte[] buffer) {
        return doubleToBytes(d, buffer, 0);
    }

    /**
     * This function converts a double into its corresponding byte format and inserts
     * it into the specified buffer at the specified index.
     *
     * @param s The double to be converted.
     * @param buffer The byte array.
     * @param index The index in the array to begin inserting bytes.
     * @return The number of bytes inserted.
     */
    static public int doubleToBytes(double s, byte[] buffer, int index) {
        long bits = Double.doubleToRawLongBits(s);
        int length = longToBytes(bits, buffer, index);
        return length;
    }

    /**
     * This function converts a long to its corresponding byte array format.
     *
     * @param l The long to be converted.
     * @return The corresponding byte array.
     */
    static public byte[] longToBytes(long l) {
        byte[] buffer = new byte[8];
        longToBytes(l, buffer, 0);
        return buffer;
    }

    /**
     * This function converts a long into its corresponding byte format and inserts
     * it into the specified buffer.
     *
     * @param l The long to be converted.
     * @param buffer The byte array.
     * @return The number of bytes inserted.
     */
    static public int longToBytes(long l, byte[] buffer) {
        return longToBytes(l, buffer, 0);
    }

    /**
     * This function converts a long into its corresponding byte format and inserts
     * it into the specified buffer at the specified index.
     *
     * @param l The long to be converted.
     * @param buffer The byte array.
     * @param index The index in the array to begin inserting bytes.
     * @return The number of bytes inserted.
     */
    static public int longToBytes(long l, byte[] buffer, int index) {
        int length = buffer.length - index;
        if (length > 8)
            length = 8;
        for (int i = 0; i < length; i++) {
            buffer[index + length - i - 1] = (byte) (l >> (i * 8));
        }
        return length;
    }
}

Related

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