class 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");
        }

        // make sure this address is always an even length
        if (address.length() % 2 != 0) {
            // add an extra F onto the end
            address = address + "F";
        }

        // calculate length of final byte[]
        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));
            // reverse the digits since its BCD encoded
            chunk.reverse();
            // get the byte
            byte[] bytes = HexUtil.toByteArray(chunk.toString());
            //byte[] bytes = ByteUtil.decodeHex(chunk.toString(), 2);
            // add this byte onto our total
            bcd[i] = bytes[0];
        }

        return bcd;
    }

}