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
9 * VTT Technical Research Centre of Finland - initial API and implementation
\r
10 *******************************************************************************/
\r
12 * Created on Jan 21, 2005
\r
14 * Copyright Toni Kalajainen
\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
20 * http://www.apache.org/licenses/LICENSE-2.0
\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
28 package org.simantics.utils.bytes;
\r
31 * Big Endian float <-> byte array conversions
\r
32 * Motorola order, Network order
\r
34 * @author Toni Kalajainen
\r
36 public class BEFloat {
\r
39 * Convert float to byte array
\r
40 * @param l float value
\r
41 * @return byte array
\r
43 public static byte[] toBytes(float value)
\r
45 byte array[] = new byte[4];
\r
46 int l = Float.floatToIntBits(value);
\r
47 array[0] = (byte) (l & 0xff);
\r
48 array[1] = (byte) ((l >> 8) & 0xff);
\r
49 array[2] = (byte) ((l >> 16) & 0xff);
\r
50 array[3] = (byte) ((l >> 24) & 0xff);
\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
60 public static void write(float value, byte array[], int offset)
\r
62 if (offset+4>array.length)
\r
63 throw new IndexOutOfBoundsException();
\r
64 int l = Float.floatToIntBits(value);
\r
65 array[0 + offset] = (byte) (l & 0xff);
\r
66 array[1 + offset] = (byte) (l >> 8);
\r
67 array[2 + offset] = (byte) (l >> 16);
\r
68 array[3 + offset] = (byte) (l >> 24);
\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
77 public static void write(float value, byte array[])
\r
80 throw new IndexOutOfBoundsException();
\r
81 int l = Float.floatToIntBits(value);
\r
82 array[0] = (byte) (l & 0xff);
\r
83 array[1] = (byte) (l >> 8);
\r
84 array[2] = (byte) (l >> 16);
\r
85 array[3] = (byte) (l >> 24);
\r
89 * read float value from byte array
\r
90 * @param array the array
\r
91 * @param offset offset
\r
94 public static float toFloat(byte array[], int offset)
\r
96 if (offset+4>array.length)
\r
97 throw new IndexOutOfBoundsException();
\r
99 Float.intBitsToFloat(
\r
100 ( ((int) array[0 + offset] & 0xFF) ) |
\r
101 ( ((int) array[1 + offset] & 0xFF) << 8) |
\r
102 ( ((int) array[2 + offset] & 0xFF) << 16) |
\r
103 ( ((int) array[3 + offset] & 0xFF) << 24));
\r
107 * read float value from byte array
\r
108 * @param array the array
\r
109 * @return the value
\r
111 public static float toFloat(byte array[])
\r
113 if (4>array.length)
\r
114 throw new IndexOutOfBoundsException();
\r
117 Float.intBitsToFloat(
\r
118 ( ((int) array[0] & 0xFF) ) |
\r
119 ( ((int) array[1] & 0xFF) << 8) |
\r
120 ( ((int) array[2] & 0xFF) << 16) |
\r
121 ( ((int) array[3] & 0xFF) << 24));
\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 = -123.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
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
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
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
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
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