Domanda

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;
    }
}
È stato utile?

Soluzione

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.

Altri suggerimenti

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

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;
  }
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top