Here you can find the source of doubleToBytes(double d)
Parameter | Description |
---|---|
d | The double to be converted. |
static public byte[] doubleToBytes(double d)
//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; } }