1 /*******************************************************************************
\r
2 * Copyright (c) 2007- VTT Technical Research Centre of Finland.
\r
3 * All rights reserved. This program and the accompanying materials
\r
4 * are made available under the terms of the Eclipse Public License v1.0
\r
5 * which accompanies this distribution, and is available at
\r
6 * http://www.eclipse.org/legal/epl-v10.html
\r
9 * VTT Technical Research Centre of Finland - initial API and implementation
\r
10 *******************************************************************************/
\r
12 * Created on Jan 21, 2005
\r
14 * Copyright Toni Kalajainen
\r
16 * Licensed under the Apache License, Version 2.0 (the "License");
\r
17 * you may not use this file except in compliance with the License.
\r
18 * You may obtain a copy of the License at
\r
20 * http://www.apache.org/licenses/LICENSE-2.0
\r
22 * Unless required by applicable law or agreed to in writing, software
\r
23 * distributed under the License is distributed on an "AS IS" BASIS,
\r
24 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
25 * See the License for the specific language governing permissions and
\r
26 * limitations under the License.
\r
28 package org.simantics.utils.bytes;
\r
31 * Big Endian short <-> byte array conversions
\r
32 * Motorola order, Network order
\r
34 * @author Toni Kalajainen
\r
36 public class BEShort {
\r
39 * Convert short to byte array
\r
40 * @param l short value
\r
41 * @return byte array
\r
43 public static byte[] toBytes(short value)
\r
45 byte array[] = new byte[2];
\r
46 array[0] = (byte) (value & 0xff);
\r
47 array[1] = (byte) ((value >> 8) & 0xff);
\r
52 * Write short value to byte array
\r
53 * @param value the short value
\r
54 * @param array the byte array
\r
55 * @param offset the offset
\r
57 public static void write(short value, byte array[], int offset)
\r
59 if (offset+2>array.length)
\r
60 throw new IndexOutOfBoundsException();
\r
61 array[0 + offset] = (byte) (value & 0xff);
\r
62 array[1 + offset] = (byte) (value >> 8);
\r
66 * Write short value to byte array
\r
67 * @param value the short value
\r
68 * @param array the byte array
\r
69 * @param offset the offset
\r
71 public static void write(short value, byte array[])
\r
74 throw new IndexOutOfBoundsException();
\r
75 array[0] = (byte) (value & 0xff);
\r
76 array[1] = (byte) (value >> 8);
\r
79 * read short value from byte array
\r
80 * @param array the array
\r
81 * @param offset offset
\r
84 public static short toShort(byte array[], int offset)
\r
86 if (offset+2>array.length)
\r
87 throw new IndexOutOfBoundsException();
\r
88 short value = (short)
\r
90 ( ((short) array[0 + offset] & 0xFF) ) |
\r
91 ( ((short) array[1 + offset] & 0xFF) << 8)
\r
97 * read short value from byte array
\r
98 * @param array the array
\r
101 public static short toShort(byte array[])
\r
103 if (2>array.length)
\r
104 throw new IndexOutOfBoundsException();
\r
105 short value = (short)
\r
107 ( ((short) array[0] & 0xFF) ) |
\r
108 ( ((short) array[1] & 0xFF) << 8)
\r
114 * read short value from byte array
\r
115 * @param array the array
\r
116 * @param offset offset
\r
117 * @return the value
\r
119 public static int toInt(byte array[], int offset)
\r
121 if (offset+2>array.length)
\r
122 throw new IndexOutOfBoundsException();
\r
123 short value = (short)
\r
125 ( ((short) array[0 + offset] & 0xFF) ) |
\r
126 ( ((short) array[1 + offset] & 0xFF) << 8)
\r
134 public static void main(String[] args) {
\r
135 System.out.println("min="+Short.MIN_VALUE+" max="+Short.MAX_VALUE);
\r
136 short value = (short) -513;
\r
137 byte array[] = toBytes(value);
\r
138 System.out.print(value);
\r
139 System.out.print(" = ");
\r
140 printByteArray(array);
\r
141 System.out.println();
\r
143 write(value, array, 0);
\r
144 System.out.print(value);
\r
145 System.out.print(" = ");
\r
146 printByteArray(array);
\r
147 System.out.println();
\r
149 write(value, array);
\r
150 System.out.print(value);
\r
151 System.out.print(" = ");
\r
152 printByteArray(array);
\r
153 System.out.println();
\r
155 value = toShort(array, 0);
\r
156 printByteArray(array);
\r
157 System.out.print(" = ");
\r
158 System.out.print(value);
\r
159 System.out.println();
\r
161 value = toShort(array);
\r
162 printByteArray(array);
\r
163 System.out.print(" = ");
\r
164 System.out.print(value);
\r
165 System.out.println();
\r
168 public static void printByteArray(byte array[]) {
\r
169 for (short i=0; i<array.length; i++) {
\r
170 System.out.print(array[i] & 0xff);
\r
171 if (i<array.length-1)
\r
172 System.out.print(",");
\r