]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.utils/src/org/simantics/utils/bytes/LEFloat.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.utils / src / org / simantics / utils / bytes / LEFloat.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 float <-> byte array conversions\r
32  * Intel order\r
33  *\r
34  * @author Toni Kalajainen\r
35  */\r
36 public class LEFloat {\r
37         \r
38         /**\r
39          * Convert float to byte array\r
40          * @param l float value\r
41          * @return byte array\r
42          */\r
43         public static byte[] toBytes(float value)\r
44         {\r
45                 byte array[] = new byte[4];\r
46                 int l = Float.floatToIntBits(value);\r
47                 array[3] = (byte) (l & 0xff);\r
48                 array[2] = (byte) ((l >> 8) & 0xff);\r
49                 array[1] = (byte) ((l >> 16) & 0xff);\r
50                 array[0] = (byte) ((l >> 24) & 0xff);\r
51                 return array;\r
52         }\r
53         \r
54         /**\r
55          * Write float value to byte array\r
56          * @param value the float value\r
57          * @param array the byte array\r
58          * @param offset the offset\r
59          */\r
60         public static void write(float value, byte array[], int offset)\r
61         {\r
62                 if (offset+4>array.length)\r
63                         throw new IndexOutOfBoundsException();          \r
64                 int l = Float.floatToIntBits(value);\r
65                 array[3 + offset] = (byte) (l & 0xff);\r
66                 array[2 + offset] = (byte) (l >> 8);\r
67                 array[1 + offset] = (byte) (l >> 16);\r
68                 array[0 + offset] = (byte) (l >> 24);\r
69         }\r
70         \r
71         /**\r
72          * Write float value to byte array\r
73          * @param value the float value\r
74          * @param array the byte array\r
75          * @param offset the offset\r
76          */\r
77         public static void write(float value, byte array[])\r
78         {\r
79                 if (array.length<4)\r
80                         throw new IndexOutOfBoundsException();          \r
81                 int l = Float.floatToIntBits(value);\r
82                 array[3] = (byte) (l & 0xff);\r
83                 array[2] = (byte) (l >> 8);\r
84                 array[1] = (byte) (l >> 16);\r
85                 array[0] = (byte) (l >> 24);\r
86         }       \r
87         \r
88         /**\r
89          * read float value from byte array\r
90          * @param array the array\r
91          * @param offset offset\r
92          * @return the value\r
93          */\r
94         public static float toFloat(byte array[], int offset)\r
95         {\r
96                 if (offset+4>array.length)\r
97                         throw new IndexOutOfBoundsException();          \r
98                 return \r
99                         Float.intBitsToFloat(\r
100                         ( ((int) array[3 + offset] & 0xFF) ) |\r
101                         ( ((int) array[2 + offset] & 0xFF) << 8) |\r
102                         ( ((int) array[1 + offset] & 0xFF) << 16) | \r
103                         ( ((int) array[0 + offset] & 0xFF) << 24)); \r
104         }\r
105         \r
106         /**\r
107          * read float value from byte array\r
108          * @param array the array\r
109          * @return the value\r
110          */\r
111         public static float toFloat(byte array[])\r
112         {\r
113                 if (4>array.length)\r
114                         throw new IndexOutOfBoundsException();          \r
115 \r
116                 return \r
117                 Float.intBitsToFloat(\r
118                 ( ((int) array[3] & 0xFF) ) |\r
119                 ( ((int) array[2] & 0xFF) << 8) |\r
120                 ( ((int) array[1] & 0xFF) << 16) | \r
121                 ( ((int) array[0] & 0xFF) << 24)); \r
122         }\r
123 \r
124         /**\r
125          * Test cases\r
126          * @param args\r
127          */\r
128         public static void main(String[] args) {\r
129                 System.out.println("min="+Float.MIN_VALUE+" max="+Float.MAX_VALUE);\r
130                 float value = -0.123123123f;\r
131                 byte array[] = toBytes(value);\r
132                 System.out.print(value);\r
133                 System.out.print(" = ");\r
134                 printByteArray(array);\r
135                 System.out.println();\r
136                 \r
137                 write(value, array, 0);\r
138                 System.out.print(value);\r
139                 System.out.print(" = ");\r
140                 printByteArray(array);\r
141                 System.out.println();\r
142                 \r
143                 write(value, array);\r
144                 System.out.print(value);\r
145                 System.out.print(" = ");\r
146                 printByteArray(array);\r
147                 System.out.println();\r
148                 \r
149                 value = toFloat(array, 0);\r
150                 printByteArray(array);\r
151                 System.out.print(" = ");\r
152                 System.out.print(value);\r
153                 System.out.println();\r
154                                 \r
155                 value = toFloat(array);\r
156                 printByteArray(array);\r
157                 System.out.print(" = ");\r
158                 System.out.print(value);\r
159                 System.out.println();\r
160                 \r
161         }\r
162         \r
163         public static void printByteArray(byte array[]) {\r
164                 for (int i=0; i<array.length; i++) {\r
165                         System.out.print(array[i] & 0xff);\r
166                         if (i<array.length-1) \r
167                                 System.out.print(",");\r
168                 }\r
169         }\r
170 }\r