]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.utils/src/org/simantics/utils/bytes/BEInt.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.utils / src / org / simantics / utils / bytes / BEInt.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  * Big Endian int <-> byte array conversions
32  * Motorola order, Network order
33  *
34  * @author Toni Kalajainen
35  */
36 public class BEInt {
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[0] = (byte) (value & 0xff);
47                 array[1] = (byte) ((value >> 8) & 0xff);
48                 array[2] = (byte) ((value >> 16) & 0xff);
49                 array[3] = (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[0 + offset] = (byte) (value & 0xff);
64                 array[1 + offset] = (byte) (value >> 8);
65                 array[2 + offset] = (byte) (value >> 16);
66                 array[3 + 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[0] = (byte) (value & 0xff);
80                 array[1] = (byte) (value >> 8);
81                 array[2] = (byte) (value >> 16);
82                 array[3] = (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[0 + offset] & 0xFF) ) |
97                         ( ((int) array[1 + offset] & 0xFF) << 8) |
98                         ( ((int) array[2 + offset] & 0xFF) << 16) | 
99                         ( ((int) array[3 + offset] & 0xFF) << 24); 
100                 return value;
101         }
102
103         /**
104          * read int value from byte array
105          * @param array the array
106          * @param offset offset
107          * @return the value
108          */
109         public static int toInt(byte array[])
110         {
111                 if (4>array.length)
112                         throw new IndexOutOfBoundsException();          
113                 int value = 
114                         ( ((int) array[0] & 0xFF) ) |
115                         ( ((int) array[1] & 0xFF) << 8) |
116                         ( ((int) array[2] & 0xFF) << 16) | 
117                         ( ((int) array[3] & 0xFF) << 24); 
118                 return value;
119         }
120         
121         /**
122          * Test cases
123          * @param args
124          */
125         public static void main(String[] args) {
126                 System.out.println("min="+Integer.MIN_VALUE+" max="+Integer.MAX_VALUE);
127                 int value = -1290000000;
128                 byte array[] = toBytes(value);
129                 System.out.print(value);
130                 System.out.print(" = ");
131                 printByteArray(array);
132                 System.out.println();
133                 
134                 write(value, array, 0);
135                 System.out.print(value);
136                 System.out.print(" = ");
137                 printByteArray(array);
138                 System.out.println();
139                 
140                 write(value, array);
141                 System.out.print(value);
142                 System.out.print(" = ");
143                 printByteArray(array);
144                 System.out.println();
145                 
146                 value = toInt(array, 0);
147                 printByteArray(array);
148                 System.out.print(" = ");
149                 System.out.print(value);
150                 System.out.println();
151                                 
152                 value = toInt(array);
153                 printByteArray(array);
154                 System.out.print(" = ");
155                 System.out.print(value);
156                 System.out.println();
157                 
158         }
159         
160         public static void printByteArray(byte array[]) {
161                 for (int i=0; i<array.length; i++) {
162                         System.out.print(array[i] & 0xff);
163                         if (i<array.length-1) 
164                                 System.out.print(",");
165                 }
166         }
167 }