IntegerBCDCodec implements BlockCodec<Integer> { @Override public byte[] encode(Integer data, int bitLength) throws BlockCodecException { byte[] value = IntegerUtils.bcdValue(data); int cnt = bitLength / 8; if (value.length > cnt) { throw new BlockCodecException("integer(BCD) encode failure. bit count of '" + data + "' is not " + bitLength + "."); } byte[] result = new byte[cnt]; int offset = cnt - value.length; for (int i = 0; i < value.length; i++) { result[offset + i] = value[i]; } return result; }  @Override Integer zeroValue(); @Override Integer decode(byte[] data, int bitLength); @Override byte[] encode(Integer data, int bitLength); @Override void reset(); @Override String getValueType();  }
@Test public void testEncode() throws Exception { IntegerBCDCodec codec = new IntegerBCDCodec(); Assert.assertArrayEquals(new byte[] { (byte) 0x01, (byte) 0x82 }, codec.encode(182, 16)); Assert.assertArrayEquals(new byte[] { (byte) 0x91, (byte) 0x82 }, codec.encode(9182, 16)); Assert.assertArrayEquals(new byte[] { (byte) 0x00, (byte) 0x82 }, codec.encode(82, 16)); Assert.assertArrayEquals(new byte[] { (byte) 0x91, (byte) 0x00 }, codec.encode(9100, 16)); }