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
9 * VTT Technical Research Centre of Finland - initial API and implementation
10 *******************************************************************************/
12 * Created on Jan 21, 2005
14 * Copyright Toni Kalajainen
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
20 * http://www.apache.org/licenses/LICENSE-2.0
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.
28 package org.simantics.utils.bytes;
31 * Little Endian int <-> byte array conversions
34 * @author Toni Kalajainen
39 * Convert int to byte array
43 public static byte[] toBytes(int value)
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);
54 * Write int value to byte array
55 * @param value the int value
56 * @param array the byte array
57 * @param offset the offset
59 public static void write(int value, byte array[], int offset)
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);
70 * Write int value to byte array
71 * @param value the int value
72 * @param array the byte array
73 * @param offset the offset
75 public static void write(int value, byte array[])
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);
86 * read int value from byte array
87 * @param array the array
88 * @param offset offset
91 public static int toInt(byte array[], int offset)
93 if (offset+4>array.length)
94 throw new IndexOutOfBoundsException();
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);
104 * read int value from byte array
105 * @param array the array
108 public static int toInt(byte array[])
111 throw new IndexOutOfBoundsException();
113 ( ((int) array[3] & 0xFF) ) |
114 ( ((int) array[2] & 0xFF) << 8) |
115 ( ((int) array[1] & 0xFF) << 16) |
116 ( ((int) array[0] & 0xFF) << 24);
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();
133 write(value, array, 0);
134 System.out.print(value);
135 System.out.print(" = ");
136 printByteArray(array);
137 System.out.println();
139 value = toInt(array, 0);
140 printByteArray(array);
141 System.out.print(" = ");
142 System.out.print(value);
143 System.out.println();
145 value = toInt(array);
146 printByteArray(array);
147 System.out.print(" = ");
148 System.out.print(value);
149 System.out.println();
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(",");