@Test public void integerToBytes() { TestUtils.assertEquals(new byte[] {0x00}, ConversionUtils.integerToBytes(0, 1)); TestUtils.assertEquals(new byte[] {0x01}, ConversionUtils.integerToBytes(1, 1)); TestUtils.assertEquals(new byte[] {(byte) 0xff}, ConversionUtils.integerToBytes(-1, 1)); TestUtils.assertEquals(new byte[] {0x7f}, ConversionUtils.integerToBytes(127, 1)); TestUtils.assertEquals(new byte[] {(byte) 0x81}, ConversionUtils.integerToBytes(-127, 1)); TestUtils.assertEquals(new byte[] {(byte) 0x80}, ConversionUtils.integerToBytes(-128, 1)); TestUtils.assertEquals(new byte[] {0x00, 0x00}, ConversionUtils.integerToBytes(0, 2)); TestUtils.assertEquals(new byte[] {0x00, 0x01}, ConversionUtils.integerToBytes(1, 2)); TestUtils.assertEquals(new byte[] {0x00, 0x7f}, ConversionUtils.integerToBytes(127, 2)); TestUtils.assertEquals(new byte[] {0x00, (byte) 0x80}, ConversionUtils.integerToBytes(128, 2)); TestUtils.assertEquals(new byte[] {0x00, (byte) 0xff}, ConversionUtils.integerToBytes(255, 2)); TestUtils.assertEquals(new byte[] {0x01, 0x00}, ConversionUtils.integerToBytes(256, 2)); TestUtils.assertEquals(new byte[] {0x01, 0x01}, ConversionUtils.integerToBytes(257, 2)); TestUtils.assertEquals(new byte[] {(byte) 0xff, (byte) 0xff}, ConversionUtils.integerToBytes(-1, 2)); TestUtils.assertEquals(new byte[] {(byte) 0xff, (byte) 0x01}, ConversionUtils.integerToBytes(-255, 2)); TestUtils.assertEquals(new byte[] {(byte) 0xff, (byte) 0x00}, ConversionUtils.integerToBytes(-256, 2)); TestUtils.assertEquals(new byte[] {(byte) 0xfe, (byte) 0xff}, ConversionUtils.integerToBytes(-257, 2)); TestUtils.assertEquals(new byte[] {0x00, 0x00, 0x00, 0x01}, ConversionUtils.integerToBytes(0x00000001, 4)); TestUtils.assertEquals(new byte[] {0x11, 0x22, 0x33, 0x44}, ConversionUtils.integerToBytes(0x11223344, 4)); TestUtils.assertEquals(new byte[] {0x7f, (byte) 0xff, (byte) 0xff, (byte) 0xff}, ConversionUtils.integerToBytes(Integer.MAX_VALUE, 4)); TestUtils.assertEquals(new byte[] {(byte) 0x80, 0x00, 0x00, 0x00}, ConversionUtils.integerToBytes(Integer.MIN_VALUE, 4)); TestUtils.assertEquals(new byte[] {(byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff}, ConversionUtils.integerToBytes(0xffffffffL, 4)); TestUtils.assertEquals(new byte[] {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, (byte) 0x88}, ConversionUtils.integerToBytes(0x1122334455667788L, 8)); }
public static byte[] integerToBytes(long value, int length) { final byte[] data = new byte[length]; for (int i = 0; i < length; i++) { data[i] = (byte) (value >> (length - i - 1) * 8); } return data; }
ConversionUtils { public static byte[] integerToBytes(long value, int length) { final byte[] data = new byte[length]; for (int i = 0; i < length; i++) { data[i] = (byte) (value >> (length - i - 1) * 8); } return data; } }
ConversionUtils { public static byte[] integerToBytes(long value, int length) { final byte[] data = new byte[length]; for (int i = 0; i < length; i++) { data[i] = (byte) (value >> (length - i - 1) * 8); } return data; }  }
ConversionUtils { public static byte[] integerToBytes(long value, int length) { final byte[] data = new byte[length]; for (int i = 0; i < length; i++) { data[i] = (byte) (value >> (length - i - 1) * 8); } return data; }  static byte[] integerToBytes(long value, int length); static long bytesToSignedInteger(byte[] data); static byte bytesToI1(byte[] data); static int bytesToI2(byte[] data); static int bytesToI4(byte[] data); static long bytesToI8(byte[] data); static long bytesToUnsignedInteger(byte[] data); }
ConversionUtils { public static byte[] integerToBytes(long value, int length) { final byte[] data = new byte[length]; for (int i = 0; i < length; i++) { data[i] = (byte) (value >> (length - i - 1) * 8); } return data; }  static byte[] integerToBytes(long value, int length); static long bytesToSignedInteger(byte[] data); static byte bytesToI1(byte[] data); static int bytesToI2(byte[] data); static int bytesToI4(byte[] data); static long bytesToI8(byte[] data); static long bytesToUnsignedInteger(byte[] data);  }