]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.utils/src/org/simantics/utils/bytes/BEFloat.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.utils / src / org / simantics / utils / bytes / BEFloat.java
index bf305223b9c852288ca9f324895541db4337da73..fa98910bbcff3328290b3bb88d0dfb96460f816f 100644 (file)
-/*******************************************************************************\r
- * Copyright (c) 2007- VTT Technical Research Centre of Finland.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- *     VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-/*\r
- * Created on Jan 21, 2005\r
- * \r
- * Copyright Toni Kalajainen\r
- * \r
- * Licensed under the Apache License, Version 2.0 (the "License");\r
- * you may not use this file except in compliance with the License.\r
- * You may obtain a copy of the License at\r
- *\r
- *     http://www.apache.org/licenses/LICENSE-2.0\r
- *\r
- * Unless required by applicable law or agreed to in writing, software\r
- * distributed under the License is distributed on an "AS IS" BASIS,\r
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- * See the License for the specific language governing permissions and\r
- * limitations under the License.\r
- */\r
-package org.simantics.utils.bytes;\r
-\r
-/**\r
- * Big Endian float <-> byte array conversions\r
- * Motorola order, Network order\r
- *\r
- * @author Toni Kalajainen\r
- */\r
-public class BEFloat {\r
-       \r
-       /**\r
-        * Convert float to byte array\r
-        * @param l float value\r
-        * @return byte array\r
-        */\r
-       public static byte[] toBytes(float value)\r
-       {\r
-               byte array[] = new byte[4];\r
-               int l = Float.floatToIntBits(value);\r
-               array[0] = (byte) (l & 0xff);\r
-               array[1] = (byte) ((l >> 8) & 0xff);\r
-               array[2] = (byte) ((l >> 16) & 0xff);\r
-               array[3] = (byte) ((l >> 24) & 0xff);\r
-               return array;\r
-       }\r
-       \r
-       /**\r
-        * Write float value to byte array\r
-        * @param value the float value\r
-        * @param array the byte array\r
-        * @param offset the offset\r
-        */\r
-       public static void write(float value, byte array[], int offset)\r
-       {\r
-               if (offset+4>array.length)\r
-                       throw new IndexOutOfBoundsException();          \r
-               int l = Float.floatToIntBits(value);\r
-               array[0 + offset] = (byte) (l & 0xff);\r
-               array[1 + offset] = (byte) (l >> 8);\r
-               array[2 + offset] = (byte) (l >> 16);\r
-               array[3 + offset] = (byte) (l >> 24);\r
-       }\r
-       \r
-       /**\r
-        * Write float value to byte array\r
-        * @param value the float value\r
-        * @param array the byte array\r
-        * @param offset the offset\r
-        */\r
-       public static void write(float value, byte array[])\r
-       {\r
-               if (array.length<4)\r
-                       throw new IndexOutOfBoundsException();          \r
-               int l = Float.floatToIntBits(value);\r
-               array[0] = (byte) (l & 0xff);\r
-               array[1] = (byte) (l >> 8);\r
-               array[2] = (byte) (l >> 16);\r
-               array[3] = (byte) (l >> 24);\r
-       }       \r
-       \r
-       /**\r
-        * read float value from byte array\r
-        * @param array the array\r
-        * @param offset offset\r
-        * @return the value\r
-        */\r
-       public static float toFloat(byte array[], int offset)\r
-       {\r
-               if (offset+4>array.length)\r
-                       throw new IndexOutOfBoundsException();          \r
-               return \r
-                       Float.intBitsToFloat(\r
-                       ( ((int) array[0 + offset] & 0xFF) ) |\r
-                       ( ((int) array[1 + offset] & 0xFF) << 8) |\r
-                       ( ((int) array[2 + offset] & 0xFF) << 16) | \r
-                       ( ((int) array[3 + offset] & 0xFF) << 24)); \r
-       }\r
-       \r
-       /**\r
-        * read float value from byte array\r
-        * @param array the array\r
-        * @return the value\r
-        */\r
-       public static float toFloat(byte array[])\r
-       {\r
-               if (4>array.length)\r
-                       throw new IndexOutOfBoundsException();          \r
-\r
-               return \r
-               Float.intBitsToFloat(\r
-               ( ((int) array[0] & 0xFF) ) |\r
-               ( ((int) array[1] & 0xFF) << 8) |\r
-               ( ((int) array[2] & 0xFF) << 16) | \r
-               ( ((int) array[3] & 0xFF) << 24)); \r
-       }\r
-\r
-       /**\r
-        * Test cases\r
-        * @param args\r
-        */\r
-       public static void main(String[] args) {\r
-               System.out.println("min="+Float.MIN_VALUE+" max="+Float.MAX_VALUE);\r
-               float value = -123.123123123f;\r
-               byte array[] = toBytes(value);\r
-               System.out.print(value);\r
-               System.out.print(" = ");\r
-               printByteArray(array);\r
-               System.out.println();\r
-               \r
-               write(value, array, 0);\r
-               System.out.print(value);\r
-               System.out.print(" = ");\r
-               printByteArray(array);\r
-               System.out.println();\r
-               \r
-               write(value, array);\r
-               System.out.print(value);\r
-               System.out.print(" = ");\r
-               printByteArray(array);\r
-               System.out.println();\r
-               \r
-               value = toFloat(array, 0);\r
-               printByteArray(array);\r
-               System.out.print(" = ");\r
-               System.out.print(value);\r
-               System.out.println();\r
-                               \r
-               value = toFloat(array);\r
-               printByteArray(array);\r
-               System.out.print(" = ");\r
-               System.out.print(value);\r
-               System.out.println();\r
-               \r
-       }\r
-       \r
-       public static void printByteArray(byte array[]) {\r
-               for (int i=0; i<array.length; i++) {\r
-                       System.out.print(array[i] & 0xff);\r
-                       if (i<array.length-1) \r
-                               System.out.print(",");\r
-               }\r
-       }\r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2007- VTT Technical Research Centre of Finland.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+/*
+ * Created on Jan 21, 2005
+ * 
+ * Copyright Toni Kalajainen
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.simantics.utils.bytes;
+
+/**
+ * Big Endian float <-> byte array conversions
+ * Motorola order, Network order
+ *
+ * @author Toni Kalajainen
+ */
+public class BEFloat {
+       
+       /**
+        * Convert float to byte array
+        * @param l float value
+        * @return byte array
+        */
+       public static byte[] toBytes(float value)
+       {
+               byte array[] = new byte[4];
+               int l = Float.floatToIntBits(value);
+               array[0] = (byte) (l & 0xff);
+               array[1] = (byte) ((l >> 8) & 0xff);
+               array[2] = (byte) ((l >> 16) & 0xff);
+               array[3] = (byte) ((l >> 24) & 0xff);
+               return array;
+       }
+       
+       /**
+        * Write float value to byte array
+        * @param value the float value
+        * @param array the byte array
+        * @param offset the offset
+        */
+       public static void write(float value, byte array[], int offset)
+       {
+               if (offset+4>array.length)
+                       throw new IndexOutOfBoundsException();          
+               int l = Float.floatToIntBits(value);
+               array[0 + offset] = (byte) (l & 0xff);
+               array[1 + offset] = (byte) (l >> 8);
+               array[2 + offset] = (byte) (l >> 16);
+               array[3 + offset] = (byte) (l >> 24);
+       }
+       
+       /**
+        * Write float value to byte array
+        * @param value the float value
+        * @param array the byte array
+        * @param offset the offset
+        */
+       public static void write(float value, byte array[])
+       {
+               if (array.length<4)
+                       throw new IndexOutOfBoundsException();          
+               int l = Float.floatToIntBits(value);
+               array[0] = (byte) (l & 0xff);
+               array[1] = (byte) (l >> 8);
+               array[2] = (byte) (l >> 16);
+               array[3] = (byte) (l >> 24);
+       }       
+       
+       /**
+        * read float value from byte array
+        * @param array the array
+        * @param offset offset
+        * @return the value
+        */
+       public static float toFloat(byte array[], int offset)
+       {
+               if (offset+4>array.length)
+                       throw new IndexOutOfBoundsException();          
+               return 
+                       Float.intBitsToFloat(
+                       ( ((int) array[0 + offset] & 0xFF) ) |
+                       ( ((int) array[1 + offset] & 0xFF) << 8) |
+                       ( ((int) array[2 + offset] & 0xFF) << 16) | 
+                       ( ((int) array[3 + offset] & 0xFF) << 24)); 
+       }
+       
+       /**
+        * read float value from byte array
+        * @param array the array
+        * @return the value
+        */
+       public static float toFloat(byte array[])
+       {
+               if (4>array.length)
+                       throw new IndexOutOfBoundsException();          
+
+               return 
+               Float.intBitsToFloat(
+               ( ((int) array[0] & 0xFF) ) |
+               ( ((int) array[1] & 0xFF) << 8) |
+               ( ((int) array[2] & 0xFF) << 16) | 
+               ( ((int) array[3] & 0xFF) << 24)); 
+       }
+
+       /**
+        * Test cases
+        * @param args
+        */
+       public static void main(String[] args) {
+               System.out.println("min="+Float.MIN_VALUE+" max="+Float.MAX_VALUE);
+               float value = -123.123123123f;
+               byte array[] = toBytes(value);
+               System.out.print(value);
+               System.out.print(" = ");
+               printByteArray(array);
+               System.out.println();
+               
+               write(value, array, 0);
+               System.out.print(value);
+               System.out.print(" = ");
+               printByteArray(array);
+               System.out.println();
+               
+               write(value, array);
+               System.out.print(value);
+               System.out.print(" = ");
+               printByteArray(array);
+               System.out.println();
+               
+               value = toFloat(array, 0);
+               printByteArray(array);
+               System.out.print(" = ");
+               System.out.print(value);
+               System.out.println();
+                               
+               value = toFloat(array);
+               printByteArray(array);
+               System.out.print(" = ");
+               System.out.print(value);
+               System.out.println();
+               
+       }
+       
+       public static void printByteArray(byte array[]) {
+               for (int i=0; i<array.length; i++) {
+                       System.out.print(array[i] & 0xff);
+                       if (i<array.length-1) 
+                               System.out.print(",");
+               }
+       }
+}