Python - Decode GSM SMS message in PDU

André picture André · Oct 23, 2013 · Viewed 9.3k times · Source

I'm receiving a PDU like message, but I'm only receiving the message "C824"

PDU example, 040C9119898392752300008010610014412202C834

04 - first octet
0C - phone number length
91 - phone number type
198983927523 - phone number
00 - protocol identifier
00 - data coding scheme
80106100144122 - time stamp
02 - message length
C834 - message, i.e. "Hi"

I need to know the format what format is this("C834") that translates to "Hi". How can I possibly translate this to human readable language?

Best Regards,

Answer

Gareth Davidson picture Gareth Davidson · Oct 23, 2013

SMS messages are 7-bit ASCII packed into an 8-bit stream. You can read about the format in section 6.1 of the specification (pdf)

In your example "C8 34" is equal to:

Hex Binary
C8  11001000
34  00110100

When split using the rules in the document it looks like this:

Hex Binary
48  1001000 most significant bit is moved to next char's least significant bit
69  1101001 
00  00

To parse this you want to do something like this:

bytes    = (0xC8, 0xF7, 0x1D, 0x14, 0x96, 0x97, 0x41, 0xF9, 0x77, 0xFD, 0x07)
number   = 0
bitcount = 0
output   = ''
for byte in bytes:
    # add data on to the end
    number = number + (byte << bitcount)
    # increase the counter
    bitcount = bitcount + 1
    # output the first 7 bits
    output = output + '%c' % (number % 128)
    # then throw them away
    number = number >> 7
    # every 7th letter you have an extra one in the buffer
    if bitcount == 7:
        output = output + '%c' % (number)
        bitcount = 0
        number = 0
print output

Not the most elegant solution but it should work. Here's a JavaScript implementation that may also help.