01: /*
02: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
03: * Distributed under the terms of either:
04: * - the common development and distribution license (CDDL), v1.0; or
05: * - the GNU Lesser General Public License, v2.1 or later
06: * $Id: IntegerUtils.java 3634 2007-01-08 21:42:24Z gbevin $
07: */
08: package com.uwyn.rife.tools;
09:
10: public class IntegerUtils {
11: public static byte[] intToBytes(int integer) {
12: byte[] bytes = new byte[4];
13:
14: bytes[0] = (byte) (integer & 0x000000ff);
15: bytes[1] = (byte) ((integer & 0x0000ff00) >> 8);
16: bytes[2] = (byte) ((integer & 0x00ff0000) >> 16);
17: bytes[3] = (byte) ((integer & 0xff000000) >> 24);
18:
19: return bytes;
20: }
21:
22: public static int bytesToInt(byte[] bytes) {
23: int q3 = bytes[3] << 24;
24: int q2 = bytes[2] << 16;
25: int q1 = bytes[1] << 8;
26: int q0 = bytes[0];
27: if (q2 < 0)
28: q2 += 16777216;
29: if (q1 < 0)
30: q1 += 65536;
31: if (q0 < 0)
32: q0 += 256;
33:
34: return q3 | q2 | q1 | q0;
35: }
36: }
|