@Test public void testToByteArrayVariable() { int len = 9; byte[] dataShort = BitBufferHelper.toByteArray(511, len); Assert.assertTrue(dataShort[0] == (byte) 255); Assert.assertTrue(dataShort[1] == (byte) 128); dataShort = BitBufferHelper.toByteArray(511, len); Assert.assertTrue(dataShort[0] == (byte) 255); Assert.assertTrue(dataShort[1] == (byte) 128); dataShort = BitBufferHelper.toByteArray((long) 511, len); Assert.assertTrue(dataShort[0] == (byte) 255); Assert.assertTrue(dataShort[1] == (byte) 128); }
public static byte[] toByteArray(Number input) { Class<? extends Number> dataType = input.getClass(); short size = 0; long longValue = input.longValue(); if (dataType == Byte.class || dataType == byte.class) { size = Byte.SIZE; } else if (dataType == Short.class || dataType == short.class) { size = Short.SIZE; } else if (dataType == Integer.class || dataType == int.class) { size = Integer.SIZE; } else if (dataType == Long.class || dataType == long.class) { size = Long.SIZE; } else { throw new IllegalArgumentException("Parameter must one of the following: Short/Int/Long\n"); } int length = size / NetUtils.NUM_BITS_IN_A_BYTE; byte[] bytes = new byte[length]; for (int i = 0; i < length; i++) { bytes[i] = (byte) (longValue >> NetUtils.NUM_BITS_IN_A_BYTE * (length - i - 1) & BYTE_MASK); } return bytes; }
BitBufferHelper { public static byte[] toByteArray(Number input) { Class<? extends Number> dataType = input.getClass(); short size = 0; long longValue = input.longValue(); if (dataType == Byte.class || dataType == byte.class) { size = Byte.SIZE; } else if (dataType == Short.class || dataType == short.class) { size = Short.SIZE; } else if (dataType == Integer.class || dataType == int.class) { size = Integer.SIZE; } else if (dataType == Long.class || dataType == long.class) { size = Long.SIZE; } else { throw new IllegalArgumentException("Parameter must one of the following: Short/Int/Long\n"); } int length = size / NetUtils.NUM_BITS_IN_A_BYTE; byte[] bytes = new byte[length]; for (int i = 0; i < length; i++) { bytes[i] = (byte) (longValue >> NetUtils.NUM_BITS_IN_A_BYTE * (length - i - 1) & BYTE_MASK); } return bytes; } }
BitBufferHelper { public static byte[] toByteArray(Number input) { Class<? extends Number> dataType = input.getClass(); short size = 0; long longValue = input.longValue(); if (dataType == Byte.class || dataType == byte.class) { size = Byte.SIZE; } else if (dataType == Short.class || dataType == short.class) { size = Short.SIZE; } else if (dataType == Integer.class || dataType == int.class) { size = Integer.SIZE; } else if (dataType == Long.class || dataType == long.class) { size = Long.SIZE; } else { throw new IllegalArgumentException("Parameter must one of the following: Short/Int/Long\n"); } int length = size / NetUtils.NUM_BITS_IN_A_BYTE; byte[] bytes = new byte[length]; for (int i = 0; i < length; i++) { bytes[i] = (byte) (longValue >> NetUtils.NUM_BITS_IN_A_BYTE * (length - i - 1) & BYTE_MASK); } return bytes; } private  BitBufferHelper(); }
BitBufferHelper { public static byte[] toByteArray(Number input) { Class<? extends Number> dataType = input.getClass(); short size = 0; long longValue = input.longValue(); if (dataType == Byte.class || dataType == byte.class) { size = Byte.SIZE; } else if (dataType == Short.class || dataType == short.class) { size = Short.SIZE; } else if (dataType == Integer.class || dataType == int.class) { size = Integer.SIZE; } else if (dataType == Long.class || dataType == long.class) { size = Long.SIZE; } else { throw new IllegalArgumentException("Parameter must one of the following: Short/Int/Long\n"); } int length = size / NetUtils.NUM_BITS_IN_A_BYTE; byte[] bytes = new byte[length]; for (int i = 0; i < length; i++) { bytes[i] = (byte) (longValue >> NetUtils.NUM_BITS_IN_A_BYTE * (length - i - 1) & BYTE_MASK); } return bytes; } private  BitBufferHelper(); static byte getByte(byte[] data); static short getShort(byte[] data, int numBits); static short getShort(byte[] data); static int getInt(byte[] data, int numBits); static int getInt(byte[] data); static long getLong(byte[] data, int numBits); static long getLong(byte[] data); static byte[] getBits(byte[] data, int startOffset, int numBits); static void setByte(byte[] data, byte input, int startOffset, int numBits); static void setBytes(byte[] data, byte[] input, int startOffset, int numBits); static int getMSBMask(int numBits); static int getLSBMask(int numBits); static long toNumber(byte[] array); static long toNumber(@Nonnull byte[] array, int numBits); static byte[] toByteArray(Number input); static byte[] toByteArray(Number input, int numBits); static byte[] shiftBitsToMSB(byte[] inputBytes, int numBits); static byte[] shiftBitsToLSB(byte[] inputBytes, int numBits); static void insertBits(byte[] data, byte[] inputdataLSB, int startOffset, int numBits); static void checkExceptions(byte[] data, int startOffset, int numBits); }
BitBufferHelper { public static byte[] toByteArray(Number input) { Class<? extends Number> dataType = input.getClass(); short size = 0; long longValue = input.longValue(); if (dataType == Byte.class || dataType == byte.class) { size = Byte.SIZE; } else if (dataType == Short.class || dataType == short.class) { size = Short.SIZE; } else if (dataType == Integer.class || dataType == int.class) { size = Integer.SIZE; } else if (dataType == Long.class || dataType == long.class) { size = Long.SIZE; } else { throw new IllegalArgumentException("Parameter must one of the following: Short/Int/Long\n"); } int length = size / NetUtils.NUM_BITS_IN_A_BYTE; byte[] bytes = new byte[length]; for (int i = 0; i < length; i++) { bytes[i] = (byte) (longValue >> NetUtils.NUM_BITS_IN_A_BYTE * (length - i - 1) & BYTE_MASK); } return bytes; } private  BitBufferHelper(); static byte getByte(byte[] data); static short getShort(byte[] data, int numBits); static short getShort(byte[] data); static int getInt(byte[] data, int numBits); static int getInt(byte[] data); static long getLong(byte[] data, int numBits); static long getLong(byte[] data); static byte[] getBits(byte[] data, int startOffset, int numBits); static void setByte(byte[] data, byte input, int startOffset, int numBits); static void setBytes(byte[] data, byte[] input, int startOffset, int numBits); static int getMSBMask(int numBits); static int getLSBMask(int numBits); static long toNumber(byte[] array); static long toNumber(@Nonnull byte[] array, int numBits); static byte[] toByteArray(Number input); static byte[] toByteArray(Number input, int numBits); static byte[] shiftBitsToMSB(byte[] inputBytes, int numBits); static byte[] shiftBitsToLSB(byte[] inputBytes, int numBits); static void insertBits(byte[] data, byte[] inputdataLSB, int startOffset, int numBits); static void checkExceptions(byte[] data, int startOffset, int numBits); static final long BYTE_MASK; }