Embedded Freaks..

October 9, 2008

Decoding GSM SMS Septets

Filed under: java — Tags: — kunilkuda @ 5:18 pm

Once encoding is done (refer to this post), decoding is easy.

First, we create two tables from the encoded data (like the encoding method), but for each 8-columns, we need to add empty column.

GSM Septet Decoding Tables

GSM Septet Decoding Tables

Then:

  1. Shift the adder table 1-column to the right
  2. Fill the most left column with ‘0’ (0x00) bytes
  3. Sum the adder and the base table

Here’s the illustration:

GSM Septet Decoding Procedures

GSM Septet Decoding Procedures

For the busy ones who just want to see the code, here it is:

  /**
   * brief Decode GSM octets into message
   * param data Encoded GSM octets
   * return Message's string
   */
  public String decode(byte[] data) {
    // uncompressedData refers to compressed septets list
    ArrayList uncompressedData = new ArrayList();
    /**
     * Original function (without optimation) that works correctly
     */
    byte adderByte = 0;
    byte baseByte = 0;
    byte resultByte = 0;
    byte shiftValue = 0;
    int iData = 0;
    while (iData < data.length) {
      if (shiftValue == 0) {
        adderByte = 0;
        baseByte = (byte) ((data&#91;iData&#93; << shiftValue) & 0x7F);
        resultByte = (byte) (baseByte + adderByte);

        uncompressedData.add(new Byte(resultByte));

        shiftValue++;
        iData++;
      } else if (shiftValue >> (8 - shiftValue));
        baseByte = (byte) ((data[iData] <>> (8 - shiftValue));
        baseByte = (byte) ((data[iData] <>> (8 - shiftValue));
        baseByte = 0;
        resultByte = (byte) (baseByte + adderByte);

        uncompressedData.add(new Byte(resultByte));

        shiftValue = 0;
      }
    }
    byte[] retVal = translateToByte(
            (Byte[]) uncompressedData.toArray(new Byte[0]));

    return new String(retVal);
  }

  /**
   * brief Tranverse Bytes[] array into byte[] array
   * param data Bytes[] array
   * return byte[] array
   */
  private byte[] translateToByte(Byte[] data) {
    byte[] retVal = new byte[data.length];

    for (int i = 0; i < data.length; i++) {
      retVal&#91;i&#93; = data&#91;i&#93;.byteValue();
    }

    return retVal;
  }
&#91;/sourcecode&#93;

Here's the example of how to use it:

&#91;sourcecode language='java'&#93;
public static void main(String&#91;&#93; args) {
    byte&#91;&#93; encodedData = new byte&#91;&#93;
        { (byte)0x31, (byte)0xD9, (byte)0x8C, 0x56,
          (byte)0xB3, (byte)0xdd, 0x0 };

    System.out.println(new PduData().decode(encodedData));
}
&#91;/sourcecode&#93;

Here's the optimized version of the function:
&#91;sourcecode language='java'&#93;
public String decode(byte&#91;&#93; data) {
  // uncompressedData refers to compressed septets list
  ArrayList uncompressedData = new ArrayList();

  byte shiftValue = 0;
  int iData = 0;
  byte adderByte = 0;
  byte baseByte = 0;
  byte resultByte = 0;
  while (iData < data.length) {
    if (shiftValue == 0) {
      adderByte = 0;
    }

    if (shiftValue < 7) {
      baseByte = (byte) ((data&#91;iData&#93; <>> (8 - shiftValue));
    }

    if (shiftValue == 7) {
      baseByte = 0;

      resultByte = (byte) (baseByte + adderByte);
      uncompressedData.add(new Byte(resultByte));

      shiftValue = 0;
    }
  }

  byte[] retVal = translateToByte(
        (Byte[]) uncompressedData.toArray(new Byte[0]));

  return new String(retVal);
}
Advertisements

5 Comments »

  1. lots of errors…not a proper code

    Comment by myka — March 15, 2010 @ 11:40 pm

  2. dear this code is not working, there are a lot of errors.

    Comment by Waqar — May 10, 2010 @ 9:18 pm

  3. Hi, can anybody fix the code?
    It is very urgent.

    Thanks.

    Comment by Lusine — June 23, 2010 @ 8:17 pm

  4. Doesn’t look like an error to me..Still works too..

    Comment by kunilkuda — August 30, 2010 @ 10:54 am

  5. A code is broken…

    Comment by code — December 7, 2010 @ 5:27 am


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: