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