2

I'm receiving SMS from GSM modem in PDU format; the TP-User-Data is "C8329BFD06DDDF72363904"
and what I get is: "�2����r69", while the sent sms is "Hello World!".

Here is my java code:

    private String fromPDUText(String PDUSMSText) {
    String endoding = PDUSMSText.substring(0, 2);
    PDUSMSText = PDUSMSText.substring(18);
    byte bs[] = new byte[PDUSMSText.length() / 2];
    for(int i = 0; i < PDUSMSText.length(); i += 2) {
        bs[i / 2] = (byte) Integer.parseInt(PDUSMSText.substring(i, i + 2), 16);
    }
    try {
        String out = new String(bs, "ASCII");
    } catch(UnsupportedEncodingException e) {
        e.printStackTrace();
        return "";
    } finally {
        return out;
    }
}
YasharHND
  • 123
  • 1
  • 3
  • 13
  • Did you ever solve this? I'm looking at a similar dilemma. I've tried one of the examples below, but still get garbage. Can't figure out why! – Alex T Aug 09 '14 at 05:02

3 Answers3

1

The input is packed in 7-bits per character, which means that every 8 bytes encode 9 characters. Constructing a parser for this format can be a fun exercise or a frustrating experience, depending on how you take it. You are probably better off using a library, and a quick Google search reveals several code examples.

Joni
  • 108,737
  • 14
  • 143
  • 193
  • I'm OK with constructing a parser, but I don't know how to do this! I've googled it a lot but I don't get any clue where to start! – YasharHND Jul 31 '13 at 15:14
  • You need to use bitwise operators, for example to create a value by taking the 3 lowest bits of `a` and the 4 highest bits of `b` do `(a & 0x7) << 4) | ((b >> 3) & 0xf)` – Joni Jul 31 '13 at 18:44
  • I did my best but what I get is the following: In "C8329BFD06" (represents "Hello") the bit pattern is: 11001000 00110010 10011011 11111101 00000110 And binary values of the following characters in ASCII table is: H: 01001000 e: 01100101 l: 01101100 l: 01101100 o: 01101111 And I don't know how to interpret this bit pattern to get the mentioned characters from it! :( – YasharHND Aug 02 '13 at 14:40
1

This is how 7Bit characters are packed:
Encoding-Decoding-7-bit-User-Data-for-SMS-PDU-PDU

YasharHND
  • 123
  • 1
  • 3
  • 13
0

Personally I find it easiest to attack this kind of problem by viewing it as having a pipe where you feed 8 bits in one end and retrieve 7 bits in the other. As long as there is at least 7 bits in the pipe you read from it. When there are less than 7 bits you need to add some more so you write 8 new bits to it. So what you need is:

  1. A pipe that can hold at least 14 bits (but why be cheap? Go with a 32-bit int!).
  2. A counter keeping track of how many bits are in the pipe at any given moment.

The algorithm in pseudo code is as follows:

pipe = 0;
bitCount = 0;
while(hasMoreData())
{
  pipe |= readByte() << bitCount;
  bitCount += 8;
  while(bitCount >= 7)
  {
    writeByte(pipe & 0x7F);
    pipe >>= 7;
    bitCount -= 7;
  }
}
Andreas Magnusson
  • 7,321
  • 3
  • 31
  • 36