1 /*******************************************************************************
2 * Copyright (c) 2007- VTT Technical Research Centre of Finland.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
9 * VTT Technical Research Centre of Finland - initial API and implementation
10 *******************************************************************************/
12 * Created on Jan 21, 2005
14 * Copyright Toni Kalajainen
16 * Licensed under the Apache License, Version 2.0 (the "License");
17 * you may not use this file except in compliance with the License.
18 * You may obtain a copy of the License at
20 * http://www.apache.org/licenses/LICENSE-2.0
22 * Unless required by applicable law or agreed to in writing, software
23 * distributed under the License is distributed on an "AS IS" BASIS,
24 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25 * See the License for the specific language governing permissions and
26 * limitations under the License.
28 package org.simantics.utils.bytes;
31 * Little Endian double <-> byte array conversions
34 * @author Toni Kalajainen
36 public class LEDouble {
39 * Convert double to byte array
40 * @param l double value
43 public static byte[] toBytes(double value)
45 byte array[] = new byte[8];
46 long l = Double.doubleToRawLongBits(value);
47 array[7] = (byte) (l & 0xff);
48 array[6] = (byte) ((l >> 8) & 0xff);
49 array[5] = (byte) ((l >> 16) & 0xff);
50 array[4] = (byte) ((l >> 24) & 0xff);
51 array[3] = (byte) ((l >> 32) & 0xff);
52 array[2] = (byte) ((l >> 40) & 0xff);
53 array[1] = (byte) ((l >> 48) & 0xff);
54 array[0] = (byte) ((l >> 56) & 0xff);
59 * Write double value to byte array
60 * @param value the double value
61 * @param array the byte array
62 * @param offset the offset
64 public static void write(double value, byte array[], int offset)
66 if (offset+8>array.length)
67 throw new IndexOutOfBoundsException();
68 long l = Double.doubleToRawLongBits(value);
69 array[7 + offset] = (byte) (l & 0xff);
70 array[6 + offset] = (byte) (l >> 8);
71 array[5 + offset] = (byte) (l >> 16);
72 array[4 + offset] = (byte) (l >> 24);
73 array[3 + offset] = (byte) (l >> 32);
74 array[2 + offset] = (byte) (l >> 40);
75 array[1 + offset] = (byte) (l >> 48);
76 array[0 + offset] = (byte) (l >> 56);
80 * Write double value to byte array
81 * @param value the double value
82 * @param array the byte array
83 * @param offset the offset
85 public static void write(double value, byte array[])
88 throw new IndexOutOfBoundsException();
89 long l = Double.doubleToRawLongBits(value);
90 array[7] = (byte) (l & 0xff);
91 array[6] = (byte) (l >> 8);
92 array[5] = (byte) (l >> 16);
93 array[4] = (byte) (l >> 24);
94 array[3] = (byte) (l >> 32);
95 array[2] = (byte) (l >> 40);
96 array[1] = (byte) (l >> 48);
97 array[0] = (byte) (l >> 56);
101 * read double value from byte array
102 * @param array the array
103 * @param offset offset
106 public static double toDouble(byte array[], int offset)
108 if (offset+8>array.length)
109 throw new IndexOutOfBoundsException();
111 Double.longBitsToDouble(
112 ( ((long) array[7 + offset] & 0xFF) ) |
113 ( ((long) array[6 + offset] & 0xFF) << 8) |
114 ( ((long) array[5 + offset] & 0xFF) << 16) |
115 ( ((long) array[4 + offset] & 0xFF) << 24) |
116 ( ((long) array[3 + offset] & 0xFF) << 32) |
117 ( ((long) array[2 + offset] & 0xFF) << 40) |
118 ( ((long) array[1 + offset] & 0xFF) << 48) |
119 ( ((long) array[0 + offset] & 0xFF) << 56)
124 * read double value from byte array
125 * @param array the array
128 public static double toDouble(byte array[])
131 throw new IndexOutOfBoundsException();
134 Double.longBitsToDouble(
135 ( ((long) array[7] & 0xFF) ) |
136 ( ((long) array[6] & 0xFF) << 8) |
137 ( ((long) array[5] & 0xFF) << 16) |
138 ( ((long) array[4] & 0xFF) << 24) |
139 ( ((long) array[3] & 0xFF) << 32) |
140 ( ((long) array[2] & 0xFF) << 40) |
141 ( ((long) array[1] & 0xFF) << 48) |
142 ( ((long) array[0] & 0xFF) << 56)
150 public static void main(String[] args) {
151 System.out.println("min="+Double.MIN_VALUE+" max="+Double.MAX_VALUE);
152 double value = -123.123;
153 byte array[] = toBytes(value);
154 System.out.print(value);
155 System.out.print(" = ");
156 printByteArray(array);
157 System.out.println();
159 write(value, array, 0);
160 System.out.print(value);
161 System.out.print(" = ");
162 printByteArray(array);
163 System.out.println();
166 System.out.print(value);
167 System.out.print(" = ");
168 printByteArray(array);
169 System.out.println();
171 value = toDouble(array, 0);
172 printByteArray(array);
173 System.out.print(" = ");
174 System.out.print(value);
175 System.out.println();
177 value = toDouble(array);
178 printByteArray(array);
179 System.out.print(" = ");
180 System.out.print(value);
181 System.out.println();
185 public static void printByteArray(byte array[]) {
186 for (int i=0; i<array.length; i++) {
187 System.out.print(array[i] & 0xff);
188 if (i<array.length-1)
189 System.out.print(",");