1 package org.lsst.ccs.utilities.conv;
2
3 /**
4 ***************************************************************************
5 **
6 ** Routines for converting between numbers and byte sub-arrays
7 **
8 ** @author Owen Saxton
9 **
10 ***************************************************************************
11 */
12 public class Convert {
13
14
15 /**
16 ***************************************************************************
17 **
18 ** Convert 4-byte little-endian subarray to integer
19 **
20 ***************************************************************************
21 */
22 public static int bytesToInt(byte[] ba, int ix)
23 {
24 return (ba[ix] & 0xff) | ((ba[ix + 1] & 0xff) << 8) |
25 ((ba[ix + 2] & 0xff) << 16) | (ba[ix + 3] << 24);
26 }
27
28
29 /**
30 ***************************************************************************
31 **
32 ** Convert 4-byte big-endian subarray to integer
33 **
34 ***************************************************************************
35 */
36 public static int bytesToIntBE(byte[] ba, int ix)
37 {
38 return (ba[ix + 3] & 0xff) | ((ba[ix + 2] & 0xff) << 8) |
39 ((ba[ix + 1] & 0xff) << 16) | (ba[ix] << 24);
40 }
41
42
43 /**
44 ***************************************************************************
45 **
46 ** Convert 2-byte little-endian subarray to short
47 **
48 ***************************************************************************
49 */
50 public static short bytesToShort(byte[] ba, int ix)
51 {
52 return (short)((ba[ix] & 0xff) | (ba[ix + 1] << 8));
53 }
54
55
56 /**
57 ***************************************************************************
58 **
59 ** Convert 2-byte big-endian subarray to short
60 **
61 ***************************************************************************
62 */
63 public static short bytesToShortBE(byte[] ba, int ix)
64 {
65 return (short)((ba[ix + 1] & 0xff) | (ba[ix] << 8));
66 }
67
68
69 /**
70 ***************************************************************************
71 **
72 ** Convert 4-byte little-endian subarray to float
73 **
74 ***************************************************************************
75 */
76 public static float bytesToFloat(byte[] ba, int ix)
77 {
78 int value = (ba[ix] & 0xff) | ((ba[ix + 1] & 0xff) << 8) |
79 ((ba[ix + 2] & 0xff) << 16) | (ba[ix + 3] << 24);
80 return Float.intBitsToFloat(value);
81 }
82
83
84 /**
85 ***************************************************************************
86 **
87 ** Convert 4-byte big-endian subarray to float
88 **
89 ***************************************************************************
90 */
91 public static float bytesToFloatBE(byte[] ba, int ix)
92 {
93 int value = (ba[ix + 3] & 0xff) | ((ba[ix + 2] & 0xff) << 8) |
94 ((ba[ix + 1] & 0xff) << 16) | (ba[ix] << 24);
95 return Float.intBitsToFloat(value);
96 }
97
98
99 /**
100 ***************************************************************************
101 **
102 ** Convert integer to 4-byte little-endian subarray
103 **
104 ***************************************************************************
105 */
106 public static void intToBytes(int val, byte[] ba, int ix)
107 {
108 ba[ix] = (byte)(val);
109 ba[ix + 1] = (byte)(val >> 8);
110 ba[ix + 2] = (byte)(val >> 16);
111 ba[ix + 3] = (byte)(val >> 24);
112 }
113
114
115 /**
116 ***************************************************************************
117 **
118 ** Convert integer to 4-byte big-endian subarray
119 **
120 ***************************************************************************
121 */
122 public static void intToBytesBE(int val, byte[] ba, int ix)
123 {
124 ba[ix] = (byte)(val >> 24);
125 ba[ix + 1] = (byte)(val >> 16);
126 ba[ix + 2] = (byte)(val >> 8);
127 ba[ix + 3] = (byte)(val);
128 }
129
130
131 /**
132 ***************************************************************************
133 **
134 ** Convert short to 2-byte little-endian subarray
135 **
136 ***************************************************************************
137 */
138 public static void shortToBytes(short val, byte[] ba, int ix)
139 {
140 ba[ix] = (byte)(val);
141 ba[ix + 1] = (byte)(val >> 8);
142 }
143
144
145 /**
146 ***************************************************************************
147 **
148 ** Convert short to 2-byte big-endian subarray
149 **
150 ***************************************************************************
151 */
152 public static void shortToBytesBE(short val, byte[] ba, int ix)
153 {
154 ba[ix] = (byte)(val >> 8);
155 ba[ix + 1] = (byte)(val);
156 }
157
158
159 /**
160 ***************************************************************************
161 **
162 ** Convert float to 4-byte little-endian subarray
163 **
164 ***************************************************************************
165 */
166 public static void floatToBytes(float val, byte[] ba, int ix)
167 {
168 int value = Float.floatToIntBits(val);
169 ba[ix] = (byte)(value);
170 ba[ix + 1] = (byte)(value >> 8);
171 ba[ix + 2] = (byte)(value >> 16);
172 ba[ix + 3] = (byte)(value >> 24);
173 }
174
175
176 /**
177 ***************************************************************************
178 **
179 ** Convert float to 4-byte big-endian subarray
180 **
181 ***************************************************************************
182 */
183 public static void floatToBytesBE(float val, byte[] ba, int ix)
184 {
185 int value = Float.floatToIntBits(val);
186 ba[ix] = (byte)(value >> 24);
187 ba[ix + 1] = (byte)(value >> 16);
188 ba[ix + 2] = (byte)(value >> 8);
189 ba[ix + 3] = (byte)(value);
190 }
191
192 }