GsmUtil { public static byte[] toBcd(String address) throws NullPointerException, IllegalArgumentException { if (address == null) { throw new NullPointerException("Address cannot be null for BCD encoding"); } if (!StringUtil.containsOnlyDigits(address)) { throw new IllegalArgumentException("Address must only contain digits"); } if (address.length() % 2 != 0) { address = address + "F"; } int bytes_len = address.length() / 2; byte[] bcd = new byte[bytes_len]; for (int i = 0; i < bytes_len; i++) { StringBuilder chunk = new StringBuilder(address.substring(i * 2, (i * 2) + 2)); chunk.reverse(); byte[] bytes = HexUtil.toByteArray(chunk.toString()); bcd[i] = bytes[0]; } return bcd; }  static byte[] toBcd(String address); static byte toSubmitInfo(boolean replyPath, boolean udhIndicator,
            boolean deliveryReceipt, int validityPeriodFormat, boolean rejectDuplicates); static byte[] removeUserDataHeader(byte[] shortMessage); static byte[] getShortMessageUserData(byte[] shortMessage); static byte[] getShortMessageUserDataHeader(byte[] shortMessage); static byte[][] createConcatenatedBinaryShortMessages(byte[] binaryShortMessage, byte referenceNum);  }
@Test public void toBcdWithString() { byte[] bytes0 = GsmUtil.toBcd("13135554272"); Assert.assertArrayEquals(HexUtil.toByteArray("3131554572f2"), bytes0); byte[] bytes1 = GsmUtil.toBcd("10950"); Assert.assertArrayEquals(HexUtil.toByteArray("0159f0"), bytes1); }
@Test(expected=IllegalArgumentException.class) public void toBcdWithStringInvalidDigits() { byte[] bytes0 = GsmUtil.toBcd("131 5554272"); }