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