Nicholas Skinner

Freelance website and web application developer

8-bit SMS PDU Decoding via PHP

SMS DecodedAlong with needing to encode 8-bit SMS PDU messages via PHP I also needed to decode them, therefore after looking into what was involved I wrote my own class.

Download: PHP SMS PDU Decoder

One Response to “8-bit SMS PDU Decoding via PHP”

  1. Mattia Says:

    Hello,

    there is an error on line 257 of the code:

    $user_data = $this->retrieve_next_char($this->user_data_len * 2)

    In case of TP-UD, the length (TP-UDL) is referred at the decoded message, so if TP-UDL is 0A (so 10), SMS will have 10 7bit charactes so the TP-UD real length is (10*7bit) = 70bit, not 80bit.

    In fact, I get “PHP Fatal error: Uncaught exception ‘Exception’ with message ‘Expected length “28”does not match available data length “26”‘ in /root/install/scripts/php/lib/pdu.php:294″.

    the correct way is:
    $bytes7 = ($this->user_data_len * 7);
    $bytes8 = intval($bytes7 / 8);
    $bitOverflow = ($bytes7 % 8) != 0;
    $bytesToRead = $bytes8 + $bitOverflow;

    $user_data = $this->retrieve_next_char($bytesToRead * 2);

    So if:
    TP-UDL = 0E (14 in dec)
    TP-UD = C374F80D6AA7C36F9038CC4E03 (13 bytes; 26 in our string)
    DECODED= Ciao miao bali (14 bytes)
    $bytes7 = 14 * 7 = 98 (SMS decoded length)
    $bytes8 = intval(98/8) = 12 (byte of 8bit without bits that does not do 1 byte)
    $bitOverflow = (98%8) != 0 = 2!=0 = true (2 are the remaining bits)
    $bytesToRead = 12 + true(1) = 13 bytes to read, done!

Leave a Reply