Search code examples
javasmshexpduutf-7

Java Convert 7bit Charset Octets to Readable String (From PDU SMS)


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;
    }
}

Solution

  • 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.