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
9 * VTT Technical Research Centre of Finland - initial API and implementation
10 *******************************************************************************/
12 * Created on Jan 21, 2005
14 * Copyright Toni Kalajainen
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
20 * http://www.apache.org/licenses/LICENSE-2.0
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.
28 package org.simantics.utils.bytes;
31 * Little Endian float <-> byte array conversions
34 * @author Toni Kalajainen
36 public class LEFloat {
39 * Convert float to byte array
40 * @param l float value
43 public static byte[] toBytes(float value)
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);
55 * Write float value to byte array
56 * @param value the float value
57 * @param array the byte array
58 * @param offset the offset
60 public static void write(float value, byte array[], int offset)
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);
72 * Write float value to byte array
73 * @param value the float value
74 * @param array the byte array
75 * @param offset the offset
77 public static void write(float value, byte array[])
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);
89 * read float value from byte array
90 * @param array the array
91 * @param offset offset
94 public static float toFloat(byte array[], int offset)
96 if (offset+4>array.length)
97 throw new IndexOutOfBoundsException();
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));
107 * read float value from byte array
108 * @param array the array
111 public static float toFloat(byte array[])
114 throw new IndexOutOfBoundsException();
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));
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();
137 write(value, array, 0);
138 System.out.print(value);
139 System.out.print(" = ");
140 printByteArray(array);
141 System.out.println();
144 System.out.print(value);
145 System.out.print(" = ");
146 printByteArray(array);
147 System.out.println();
149 value = toFloat(array, 0);
150 printByteArray(array);
151 System.out.print(" = ");
152 System.out.print(value);
153 System.out.println();
155 value = toFloat(array);
156 printByteArray(array);
157 System.out.print(" = ");
158 System.out.print(value);
159 System.out.println();
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(",");