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