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