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 short <-> byte array conversions
34 * @author Toni Kalajainen
36 public class LEShort {
39 * Convert short to byte array
40 * @param l short value
43 public static byte[] toBytes(short value)
45 byte array[] = new byte[2];
46 array[1] = (byte) (value & 0xff);
47 array[0] = (byte) ((value >> 8) & 0xff);
52 * Write short value to byte array
53 * @param value the short value
54 * @param array the byte array
55 * @param offset the offset
57 public static void write(short value, byte array[], int offset)
59 if (offset+2>array.length)
60 throw new IndexOutOfBoundsException();
61 array[1 + offset] = (byte) (value & 0xff);
62 array[0 + offset] = (byte) (value >> 8);
66 * Write short value to byte array
67 * @param value the short value
68 * @param array the byte array
69 * @param offset the offset
71 public static void write(short value, byte array[])
74 throw new IndexOutOfBoundsException();
75 array[1] = (byte) (value & 0xff);
76 array[0] = (byte) (value >> 8);
80 * read short value from byte array
81 * @param array the array
82 * @param offset offset
85 public static short toShort(byte array[], int offset)
87 if (offset+2>array.length)
88 throw new IndexOutOfBoundsException();
91 ( ((short) array[1 + offset] & 0xFF) ) |
92 ( ((short) array[0 + offset] & 0xFF) << 8)
98 * read short value from byte array
99 * @param array the array
100 * @param offset offset
103 public static int toInt(byte array[], int offset)
105 if (offset+2>array.length)
106 throw new IndexOutOfBoundsException();
107 short value = (short)
109 ( ((short) array[1 + offset] & 0xFF) ) |
110 ( ((short) array[0 + offset] & 0xFF) << 8)
116 * read short value from byte array
117 * @param array the array
120 public static short toShort(byte array[])
123 throw new IndexOutOfBoundsException();
124 short value = (short)
126 ( ((short) array[1] & 0xFF) ) |
127 ( ((short) array[0] & 0xFF) << 8)
136 public static void main(String[] args) {
137 System.out.println("min="+Short.MIN_VALUE+" max="+Short.MAX_VALUE);
138 short value = (short) -513;
139 byte array[] = toBytes(value);
140 System.out.print(value);
141 System.out.print(" = ");
142 printByteArray(array);
143 System.out.println();
145 write(value, array, 0);
146 System.out.print(value);
147 System.out.print(" = ");
148 printByteArray(array);
149 System.out.println();
152 System.out.print(value);
153 System.out.print(" = ");
154 printByteArray(array);
155 System.out.println();
157 value = toShort(array, 0);
158 printByteArray(array);
159 System.out.print(" = ");
160 System.out.print(value);
161 System.out.println();
163 value = toShort(array);
164 printByteArray(array);
165 System.out.print(" = ");
166 System.out.print(value);
167 System.out.println();
170 public static void printByteArray(byte array[]) {
171 for (short i=0; i<array.length; i++) {
172 System.out.print(array[i] & 0xff);
173 if (i<array.length-1)
174 System.out.print(",");