]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.utils/src/org/simantics/utils/bytes/LEDouble.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.utils / src / org / simantics / utils / bytes / LEDouble.java
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
7  *\r
8  * Contributors:\r
9  *     VTT Technical Research Centre of Finland - initial API and implementation\r
10  *******************************************************************************/\r
11 /*\r
12  * Created on Jan 21, 2005\r
13  * \r
14  * Copyright Toni Kalajainen\r
15  * \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
19  *\r
20  *     http://www.apache.org/licenses/LICENSE-2.0\r
21  *\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
27  */\r
28 package org.simantics.utils.bytes;\r
29 \r
30 /**\r
31  * Little Endian double <-> byte array conversions\r
32  * Intel order\r
33  *\r
34  * @author Toni Kalajainen\r
35  */\r
36 public class LEDouble {\r
37         \r
38         /**\r
39          * Convert double to byte array\r
40          * @param l double value\r
41          * @return byte array\r
42          */\r
43         public static byte[] toBytes(double value)\r
44         {\r
45                 byte array[] = new byte[8];\r
46                 long l = Double.doubleToRawLongBits(value);\r
47                 array[7] = (byte) (l & 0xff);\r
48                 array[6] = (byte) ((l >> 8) & 0xff);\r
49                 array[5] = (byte) ((l >> 16) & 0xff);\r
50                 array[4] = (byte) ((l >> 24) & 0xff);\r
51                 array[3] = (byte) ((l >> 32) & 0xff);\r
52                 array[2] = (byte) ((l >> 40) & 0xff);\r
53                 array[1] = (byte) ((l >> 48) & 0xff);\r
54                 array[0] = (byte) ((l >> 56) & 0xff);\r
55                 return array;\r
56         }\r
57         \r
58         /**\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
63          */\r
64         public static void write(double value, byte array[], int offset)\r
65         {\r
66                 if (offset+8>array.length)\r
67                         throw new IndexOutOfBoundsException();          \r
68                 long l = Double.doubleToRawLongBits(value);\r
69                 array[7 + offset] = (byte) (l & 0xff);\r
70                 array[6 + offset] = (byte) (l >> 8);\r
71                 array[5 + offset] = (byte) (l >> 16);\r
72                 array[4 + offset] = (byte) (l >> 24);\r
73                 array[3 + offset] = (byte) (l >> 32);\r
74                 array[2 + offset] = (byte) (l >> 40);\r
75                 array[1 + offset] = (byte) (l >> 48);\r
76                 array[0 + offset] = (byte) (l >> 56);\r
77         }\r
78         \r
79         /**\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
84          */\r
85         public static void write(double value, byte array[])\r
86         {\r
87                 if (array.length<8)\r
88                         throw new IndexOutOfBoundsException();          \r
89                 long l = Double.doubleToRawLongBits(value);\r
90                 array[7] = (byte) (l & 0xff);\r
91                 array[6] = (byte) (l >> 8);\r
92                 array[5] = (byte) (l >> 16);\r
93                 array[4] = (byte) (l >> 24);\r
94                 array[3] = (byte) (l >> 32);\r
95                 array[2] = (byte) (l >> 40);\r
96                 array[1] = (byte) (l >> 48);\r
97                 array[0] = (byte) (l >> 56);\r
98         }       \r
99         \r
100         /**\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
105          */\r
106         public static double toDouble(byte array[], int offset)\r
107         {\r
108                 if (offset+8>array.length)\r
109                         throw new IndexOutOfBoundsException();          \r
110                 return \r
111                         Double.longBitsToDouble(\r
112                         ( ((long) array[7 + offset] & 0xFF) ) |\r
113                         ( ((long) array[6 + offset] & 0xFF) << 8) |\r
114                         ( ((long) array[5 + offset] & 0xFF) << 16) | \r
115                         ( ((long) array[4 + offset] & 0xFF) << 24) | \r
116                         ( ((long) array[3 + offset] & 0xFF) << 32) | \r
117                         ( ((long) array[2 + offset] & 0xFF) << 40) | \r
118                         ( ((long) array[1 + offset] & 0xFF) << 48) | \r
119                         ( ((long) array[0 + offset] & 0xFF) << 56)\r
120                 );\r
121         }\r
122         \r
123         /**\r
124          * read double value from byte array\r
125          * @param array the array\r
126          * @return the value\r
127          */\r
128         public static double toDouble(byte array[])\r
129         {\r
130                 if (8>array.length)\r
131                         throw new IndexOutOfBoundsException();          \r
132 \r
133                 return \r
134                 Double.longBitsToDouble(\r
135                 ( ((long) array[7] & 0xFF) ) |\r
136                 ( ((long) array[6] & 0xFF) << 8) |\r
137                 ( ((long) array[5] & 0xFF) << 16) | \r
138                 ( ((long) array[4] & 0xFF) << 24) | \r
139                 ( ((long) array[3] & 0xFF) << 32) | \r
140                 ( ((long) array[2] & 0xFF) << 40) | \r
141                 ( ((long) array[1] & 0xFF) << 48) | \r
142                 ( ((long) array[0] & 0xFF) << 56)\r
143                 );\r
144         }\r
145 \r
146         /**\r
147          * Test cases\r
148          * @param args\r
149          */\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
158                 \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
164                 \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
170                 \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
176                                 \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
182                 \r
183         }\r
184         \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
190                 }\r
191         }\r
192 }\r