JBBPUtils { public static byte[] str2bin(final String values) { return str2bin(values, JBBPBitOrder.LSB0); } private  JBBPUtils(); static byte[] strToUtf8(final String str); static String utf8ToStr(final byte[] array); static boolean isNumber(final String num); static byte[] packInt(final int value); static int packInt(final byte[] array, final JBBPIntCounter position, final int value); static int unpackInt(final byte[] array, final JBBPIntCounter position); static String array2hex(final byte[] array); static String array2bin(final byte[] array); static String array2oct(final byte[] array); static String byteArray2String(final byte[] array, final String prefix, final String delimiter, final boolean brackets, final int radix); static byte reverseBitsInByte(final byte value); static byte reverseBitsInByte(final JBBPBitNumber bitNumber, final byte value); static String bin2str(final byte[] values); static String bin2str(final byte[] values, final boolean separateBytes); static String bin2str(final byte[] values, final JBBPBitOrder bitOrder, final boolean separateBytes); static List<JBBPAbstractField> fieldsAsList(final JBBPAbstractField... fields); static byte[] str2bin(final String values); static byte[] str2bin(final String values, final JBBPBitOrder bitOrder); static String[] splitString(final String str, final char splitChar); static void assertNotNull(final Object object, final String message); static String int2msg(final int number); static String normalizeFieldNameOrPath(final String nameOrPath); static void closeQuietly(final Closeable closeable); static byte[] str2UnicodeByteArray(final JBBPByteOrder byteOrder, final String str); static byte[] reverseArray(final byte[] nullableArrayToBeInverted); static byte[] splitInteger(final int value, final boolean valueInLittleEndian, final byte[] buffer); static byte[] splitLong(final long value, final boolean valueInLittleEndian, final byte[] buffer); static byte[] concat(final byte[]... arrays); static long reverseByteOrder(long value, int numOfLowerBytesToInvert); static String double2str(final double doubleValue, final int radix); static String float2str(final float floatValue, final int radix); static String ulong2str(final long ulongValue, final int radix, final char[] charBuffer); static String ensureMinTextLength(final String text, final int neededLen, final char ch, final int mode); static String removeLeadingZeros(final String str); static String removeTrailingZeros(final String str); static boolean arrayStartsWith(final byte[] array, final byte[] str); static boolean arrayEndsWith(final byte[] array, final byte[] str); static int makeMask(final int value); static boolean equals(final Object o1, final Object o2); static final String[] ARRAY_STRING_EMPTY; static final JBBPAbstractField[] ARRAY_FIELD_EMPTY; }
@Test public void testStr2Bin_Default() { assertEquals(0, JBBPUtils.str2bin(null).length); assertArrayEquals(new byte[] {(byte) 0x80}, JBBPUtils.str2bin("10000000")); assertArrayEquals(new byte[] {(byte) 0x01}, JBBPUtils.str2bin("1")); assertArrayEquals(new byte[] {(byte) 0x80, 0x01}, JBBPUtils.str2bin("10000000X00x0Zz1")); assertArrayEquals(new byte[] {(byte) 0x80, 0x01, 0x07}, JBBPUtils.str2bin("10000000000000010111")); assertArrayEquals(new byte[] {(byte) 0x80, 0x01, 0x07}, JBBPUtils.str2bin("10000000_00000001_0111")); try { JBBPUtils.str2bin("10001021"); fail("Must throw IAE"); } catch (IllegalArgumentException ex) { } }
@Test public void testStr2Bin_LSB0() { assertEquals(0, JBBPUtils.str2bin(null, JBBPBitOrder.LSB0).length); assertArrayEquals(new byte[] {(byte) 0x80}, JBBPUtils.str2bin("10000000", JBBPBitOrder.LSB0)); assertArrayEquals(new byte[] {(byte) 0x01}, JBBPUtils.str2bin("1", JBBPBitOrder.LSB0)); assertArrayEquals(new byte[] {(byte) 0x01}, JBBPUtils.str2bin("00000001", JBBPBitOrder.LSB0)); assertArrayEquals(new byte[] {(byte) 0x80, (byte) 0x01}, JBBPUtils.str2bin("10000000X00x0Zz1", JBBPBitOrder.LSB0)); assertArrayEquals(new byte[] {(byte) 0x80, (byte) 0x01, (byte) 0x07}, JBBPUtils.str2bin("10000000000000010111", JBBPBitOrder.LSB0)); assertArrayEquals(new byte[] {(byte) 0x80, (byte) 0x01, (byte) 0x07}, JBBPUtils.str2bin("10000000_00000001_0111", JBBPBitOrder.LSB0)); try { JBBPUtils.str2bin("10001021", JBBPBitOrder.MSB0); fail("Must throw IAE"); } catch (IllegalArgumentException ex) { } }
@Test public void testStr2Bin_LSB0_1bitShift() { final byte[] array = JBBPUtils.str2bin("0 11111111 01010101 00011000 00000001", JBBPBitOrder.LSB0); assertArrayEquals(new byte[] {(byte) 0x7F, (byte) 0xAA, (byte) 0x8C, (byte) 0x0, (byte) 0x01}, array); }
@Test public void testStr2Bin_MSB() { assertEquals(0, JBBPUtils.str2bin(null, JBBPBitOrder.MSB0).length); assertArrayEquals(new byte[] {(byte) 0x01}, JBBPUtils.str2bin("1", JBBPBitOrder.MSB0)); assertArrayEquals(new byte[] {(byte) 0x80}, JBBPUtils.str2bin("00000001", JBBPBitOrder.MSB0)); assertArrayEquals(new byte[] {(byte) 0x01}, JBBPUtils.str2bin("10000000", JBBPBitOrder.MSB0)); assertArrayEquals(new byte[] {(byte) 0xA9}, JBBPUtils.str2bin("10010101", JBBPBitOrder.MSB0)); assertArrayEquals(new byte[] {(byte) 0x01, (byte) 0x80}, JBBPUtils.str2bin("10000000X00x0Zz1", JBBPBitOrder.MSB0)); assertArrayEquals(new byte[] {(byte) 0x01, (byte) 0x80, (byte) 0x0E}, JBBPUtils.str2bin("1000000000000001 0111", JBBPBitOrder.MSB0)); assertArrayEquals(new byte[] {(byte) 0x01, (byte) 0x80, (byte) 0x0E}, JBBPUtils.str2bin("10000000_00000001_0111", JBBPBitOrder.MSB0)); assertArrayEquals(new byte[] {(byte) 0x03, (byte) 0x00, (byte) 0x01, (byte) 0x01}, JBBPUtils.str2bin("1_10000000_00000001_00000001", JBBPBitOrder.MSB0)); try { JBBPUtils.str2bin("10001021", JBBPBitOrder.MSB0); fail("Must throw IAE"); } catch (IllegalArgumentException ex) { } }