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