// IntBytesConverter.java // // Informatics 102 Winter 2010 // Assignment #4 // // This class can be used to convert an int to an array of four bytes and // back again. public class IntBytesConverter { public static byte[] convertIntToBytes(int i) { return new byte[] { (byte) (i >>> 24), (byte) (i >>> 16), (byte) (i >>> 8), (byte) i }; } public static int convertBytesToInt(byte[] b) { if (b == null || b.length != 4) { throw new IllegalArgumentException( "Argument must be an array of four bytes"); } return (b[0] << 24) + ((b[1] & 0xFF) << 16) + ((b[2] & 0xFF) << 8) + (b[3] & 0xFF); } }