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 * Big Endian short <-> byte array conversions
32 * Motorola order, Network order
34 * @author Toni Kalajainen
36 public class BEShort {
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[0] = (byte) (value & 0xff);
47 array[1] = (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[0 + offset] = (byte) (value & 0xff);
62 array[1 + 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[0] = (byte) (value & 0xff);
76 array[1] = (byte) (value >> 8);
79 * read short value from byte array
80 * @param array the array
81 * @param offset offset
84 public static short toShort(byte array[], int offset)
86 if (offset+2>array.length)
87 throw new IndexOutOfBoundsException();
90 ( ((short) array[0 + offset] & 0xFF) ) |
91 ( ((short) array[1 + offset] & 0xFF) << 8)
97 * read short value from byte array
98 * @param array the array
101 public static short toShort(byte array[])
104 throw new IndexOutOfBoundsException();
105 short value = (short)
107 ( ((short) array[0] & 0xFF) ) |
108 ( ((short) array[1] & 0xFF) << 8)
114 * read short value from byte array
115 * @param array the array
116 * @param offset offset
119 public static int toInt(byte array[], int offset)
121 if (offset+2>array.length)
122 throw new IndexOutOfBoundsException();
123 short value = (short)
125 ( ((short) array[0 + offset] & 0xFF) ) |
126 ( ((short) array[1 + offset] & 0xFF) << 8)
134 public static void main(String[] args) {
135 System.out.println("min="+Short.MIN_VALUE+" max="+Short.MAX_VALUE);
136 short value = (short) -513;
137 byte array[] = toBytes(value);
138 System.out.print(value);
139 System.out.print(" = ");
140 printByteArray(array);
141 System.out.println();
143 write(value, array, 0);
144 System.out.print(value);
145 System.out.print(" = ");
146 printByteArray(array);
147 System.out.println();
150 System.out.print(value);
151 System.out.print(" = ");
152 printByteArray(array);
153 System.out.println();
155 value = toShort(array, 0);
156 printByteArray(array);
157 System.out.print(" = ");
158 System.out.print(value);
159 System.out.println();
161 value = toShort(array);
162 printByteArray(array);
163 System.out.print(" = ");
164 System.out.print(value);
165 System.out.println();
168 public static void printByteArray(byte array[]) {
169 for (short i=0; i<array.length; i++) {
170 System.out.print(array[i] & 0xff);
171 if (i<array.length-1)
172 System.out.print(",");