nFillBits = 7 - (nUdhBits % 7);
if ( (nUdhBits % 7) > 0 ) { nFillBits = 7 - (nUdhBits % 7); }
    private static byte[] encodeSeptetPdu(SmsPdu thePdu, SmsAddress theDestination, SmsAddress theSender)        throws SmsException    {        SmsUserData userData = thePdu.getUserData();        byte[] ud = userData.getData();        byte[] udh = thePdu.getUserDataHeaders();        int nUdSeptets = userData.getLength();        int nUdBits = 0;        int nUdhBytes = (udh == null) ? 0 : udh.length;        // UDH + UDHL        int nUdhBits = 0;        // UD + UDH + UDHL        int nTotalBytes = 0;        int nTotalBits = 0;        int nTotalSeptets = 0;        int nFillBits = 0;        ByteArrayOutputStream baos = new ByteArrayOutputStream(161);        try        {            // Use default SMSC            baos.write(0x00);            // UDH?            if (nUdhBytes == 0)            {                // TP-Message-Type-Indicator = SUBMIT                // TP-Reject-Duplicates = ON                // TP-Validity-Period-Format = No field                // TP-Status-Report-Request = No                // TP-User-Data-Header = No                // TP-Reply-Path = No                baos.write(0x01);            }            else            {                // +1 is for the UDHL                nUdhBits = nUdhBytes * 8;                nFillBits = 7 - (nUdhBits % 7);                // TP-Message-Type-Indicator = SUBMIT                // TP-Reject-Duplicates = ON                // TP-Validity-Period-Format = No field                // TP-Status-Report-Request = No                // TP-User-Data-Header = Yes                // TP-Reply-Path = No                baos.write(0x41);            }            nUdBits = nUdSeptets * 7;            nTotalBits = nUdSeptets * 7 + nFillBits + nUdhBits;            nTotalSeptets = nTotalBits / 7;            nTotalBytes = nTotalBits / 8;            if (nTotalBits % 8 > 0)            {                nTotalBytes += 1;            }            // TP-Message-Reference            // Leave to 0x00, MS will set it            baos.write(0x00);            // 2-12 octets            // TP-DA            // - 1:st octet - length of address (4 bits)            // - 2:nd octet            //   - myBit 7 - always 1            //   - myBit 4-6 - TON            //   - myBit 0-3 - NPI            // - n octets - BCD            writeDestinationAddress(baos, theDestination);            // TP-PID            baos.write(0x00);            // TP-DCS            // UCS, septets, language, SMS class...            baos.write(thePdu.getDcs());            // TP-VP - Optional            // Probably not needed            // UDH?            if ((udh == null) || (udh.length == 0))            {                // TP-UDL                baos.write(nUdSeptets);                // TP-UD                baos.write(ud);            }            else            {                // The whole UD PDU                byte[] fullUd = new byte[nTotalBytes];                // TP-UDL                // UDL includes the length of the UDHL                baos.write(nTotalSeptets);                // TP-UDH (including user data header length)                System.arraycopy(udh, 0, fullUd, 0, nUdhBytes);                // TP-UD                SmsPduUtil.arrayCopy(ud, 0, fullUd, nUdhBytes, nFillBits, nUdBits);                baos.write(fullUd);            }            baos.close();        }        catch (IOException ex)        {            throw new SmsException(ex.getMessage());        }        return baos.toByteArray();    }