@Test public void testToAsciiBytes() { byte[] bits = new byte[1]; String l_encoded = new String(BinaryCodec.toAsciiBytes(bits)); assertEquals("00000000", l_encoded); bits = new byte[1]; bits[0] = BIT_0; l_encoded = new String(BinaryCodec.toAsciiBytes(bits)); assertEquals("00000001", l_encoded); bits = new byte[1]; bits[0] = BIT_0 | BIT_1; l_encoded = new String(BinaryCodec.toAsciiBytes(bits)); assertEquals("00000011", l_encoded); bits = new byte[1]; bits[0] = BIT_0 | BIT_1 | BIT_2; l_encoded = new String(BinaryCodec.toAsciiBytes(bits)); assertEquals("00000111", l_encoded); bits = new byte[1]; bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3; l_encoded = new String(BinaryCodec.toAsciiBytes(bits)); assertEquals("00001111", l_encoded); bits = new byte[1]; bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4; l_encoded = new String(BinaryCodec.toAsciiBytes(bits)); assertEquals("00011111", l_encoded); bits = new byte[1]; bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5; l_encoded = new String(BinaryCodec.toAsciiBytes(bits)); assertEquals("00111111", l_encoded); bits = new byte[1]; bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6; l_encoded = new String(BinaryCodec.toAsciiBytes(bits)); assertEquals("01111111", l_encoded); bits = new byte[1]; bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); l_encoded = new String(BinaryCodec.toAsciiBytes(bits)); assertEquals("11111111", l_encoded); bits = new byte[2]; l_encoded = new String(BinaryCodec.toAsciiBytes(bits)); assertEquals("0000000000000000", l_encoded); bits = new byte[2]; bits[0] = BIT_0; l_encoded = new String(BinaryCodec.toAsciiBytes(bits)); assertEquals("0000000000000001", l_encoded); bits = new byte[2]; bits[0] = BIT_0 | BIT_1; l_encoded = new String(BinaryCodec.toAsciiBytes(bits)); assertEquals("0000000000000011", l_encoded); bits = new byte[2]; bits[0] = BIT_0 | BIT_1 | BIT_2; l_encoded = new String(BinaryCodec.toAsciiBytes(bits)); assertEquals("0000000000000111", l_encoded); bits = new byte[2]; bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3; l_encoded = new String(BinaryCodec.toAsciiBytes(bits)); assertEquals("0000000000001111", l_encoded); bits = new byte[2]; bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4; l_encoded = new String(BinaryCodec.toAsciiBytes(bits)); assertEquals("0000000000011111", l_encoded); bits = new byte[2]; bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5; l_encoded = new String(BinaryCodec.toAsciiBytes(bits)); assertEquals("0000000000111111", l_encoded); bits = new byte[2]; bits[0] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6; l_encoded = new String(BinaryCodec.toAsciiBytes(bits)); assertEquals("0000000001111111", l_encoded); bits = new byte[2]; bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); l_encoded = new String(BinaryCodec.toAsciiBytes(bits)); assertEquals("0000000011111111", l_encoded); bits = new byte[2]; bits[1] = BIT_0; bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); l_encoded = new String(BinaryCodec.toAsciiBytes(bits)); assertEquals("0000000111111111", l_encoded); bits = new byte[2]; bits[1] = BIT_0 | BIT_1; bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); l_encoded = new String(BinaryCodec.toAsciiBytes(bits)); assertEquals("0000001111111111", l_encoded); bits = new byte[2]; bits[1] = BIT_0 | BIT_1 | BIT_2; bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); l_encoded = new String(BinaryCodec.toAsciiBytes(bits)); assertEquals("0000011111111111", l_encoded); bits = new byte[2]; bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3; bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); l_encoded = new String(BinaryCodec.toAsciiBytes(bits)); assertEquals("0000111111111111", l_encoded); bits = new byte[2]; bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4; bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); l_encoded = new String(BinaryCodec.toAsciiBytes(bits)); assertEquals("0001111111111111", l_encoded); bits = new byte[2]; bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5; bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); l_encoded = new String(BinaryCodec.toAsciiBytes(bits)); assertEquals("0011111111111111", l_encoded); bits = new byte[2]; bits[1] = BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6; bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); l_encoded = new String(BinaryCodec.toAsciiBytes(bits)); assertEquals("0111111111111111", l_encoded); bits = new byte[2]; bits[0] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); bits[1] = (byte) (BIT_0 | BIT_1 | BIT_2 | BIT_3 | BIT_4 | BIT_5 | BIT_6 | BIT_7); l_encoded = new String(BinaryCodec.toAsciiBytes(bits)); assertEquals("1111111111111111", l_encoded); assertEquals(0, BinaryCodec.toAsciiBytes((byte[]) null).length); }
public static byte[] toAsciiBytes(final byte[] raw) { if (isEmpty(raw)) { return EMPTY_BYTE_ARRAY; } final byte[] l_ascii = new byte[raw.length << 3]; for (int ii = 0, jj = l_ascii.length - 1; ii < raw.length; ii++, jj -= 8) { for (int bits = 0; bits < BITS.length; ++bits) { if ((raw[ii] & BITS[bits]) == 0) { l_ascii[jj - bits] = '0'; } else { l_ascii[jj - bits] = '1'; } } } return l_ascii; }
BinaryCodec implements BinaryDecoder, BinaryEncoder { public static byte[] toAsciiBytes(final byte[] raw) { if (isEmpty(raw)) { return EMPTY_BYTE_ARRAY; } final byte[] l_ascii = new byte[raw.length << 3]; for (int ii = 0, jj = l_ascii.length - 1; ii < raw.length; ii++, jj -= 8) { for (int bits = 0; bits < BITS.length; ++bits) { if ((raw[ii] & BITS[bits]) == 0) { l_ascii[jj - bits] = '0'; } else { l_ascii[jj - bits] = '1'; } } } return l_ascii; } }
BinaryCodec implements BinaryDecoder, BinaryEncoder { public static byte[] toAsciiBytes(final byte[] raw) { if (isEmpty(raw)) { return EMPTY_BYTE_ARRAY; } final byte[] l_ascii = new byte[raw.length << 3]; for (int ii = 0, jj = l_ascii.length - 1; ii < raw.length; ii++, jj -= 8) { for (int bits = 0; bits < BITS.length; ++bits) { if ((raw[ii] & BITS[bits]) == 0) { l_ascii[jj - bits] = '0'; } else { l_ascii[jj - bits] = '1'; } } } return l_ascii; }  }
BinaryCodec implements BinaryDecoder, BinaryEncoder { public static byte[] toAsciiBytes(final byte[] raw) { if (isEmpty(raw)) { return EMPTY_BYTE_ARRAY; } final byte[] l_ascii = new byte[raw.length << 3]; for (int ii = 0, jj = l_ascii.length - 1; ii < raw.length; ii++, jj -= 8) { for (int bits = 0; bits < BITS.length; ++bits) { if ((raw[ii] & BITS[bits]) == 0) { l_ascii[jj - bits] = '0'; } else { l_ascii[jj - bits] = '1'; } } } return l_ascii; }  @Override byte[] encode(final byte[] raw); @Override Object encode(final Object raw); @Override Object decode(final Object ascii); @Override byte[] decode(final byte[] ascii); byte[] toByteArray(final String ascii); static byte[] fromAscii(final char[] ascii); static byte[] fromAscii(final byte[] ascii); static byte[] toAsciiBytes(final byte[] raw); static char[] toAsciiChars(final byte[] raw); static String toAsciiString(final byte[] raw); }
BinaryCodec implements BinaryDecoder, BinaryEncoder { public static byte[] toAsciiBytes(final byte[] raw) { if (isEmpty(raw)) { return EMPTY_BYTE_ARRAY; } final byte[] l_ascii = new byte[raw.length << 3]; for (int ii = 0, jj = l_ascii.length - 1; ii < raw.length; ii++, jj -= 8) { for (int bits = 0; bits < BITS.length; ++bits) { if ((raw[ii] & BITS[bits]) == 0) { l_ascii[jj - bits] = '0'; } else { l_ascii[jj - bits] = '1'; } } } return l_ascii; }  @Override byte[] encode(final byte[] raw); @Override Object encode(final Object raw); @Override Object decode(final Object ascii); @Override byte[] decode(final byte[] ascii); byte[] toByteArray(final String ascii); static byte[] fromAscii(final char[] ascii); static byte[] fromAscii(final byte[] ascii); static byte[] toAsciiBytes(final byte[] raw); static char[] toAsciiChars(final byte[] raw); static String toAsciiString(final byte[] raw);  }