GSM PDU decoding algorithm behaves weirdly

Thread Starter

devjeetmandal

Joined May 7, 2017
48
Hello Everyone.
As i was trying to decode PDU format texts from GSM module SIM800c i faced some weird problems. First let me tell you the information i know so that you can tell me whatever i know is right or wrong or do i need to know more about this topic.
suppose i have a text message : hello (HEX: 68 65 6C 6C 6F simultaneously)

Binary Value: (in septets)

h e l l o
68 65 6C 6C 6F

1101000 1100101 1101100 1101100 1101111


The attachment shows how to encode and decode algorithm.

newwwwww.PNG



NOTE: When we convert PDU to normal ASCII Values after 8th character there will be an extra character as we are converting it into ASCII which is septet.

C:
#include <stdio.h>
#include <string.h>

int *HexToDec(int *string,int len)
{
   int newHex[512];
   int i=0,j,temp=0x00,l;
   int mask = 0xFF;

   for (i=0,j=0,l=0;i<len/2;i++,j++,l++)
   {
    if(j==8)
  {
  newHex[l] = temp & 0x7f;
  temp = temp>>7;
  j=1;
  l+=1;
  }
  newHex[l] = ((string[i] & (mask>>j+1)) << j) | temp;
  temp = string[i] >> (7-j);
   }
   return (&newHex[0]);
}

int* PackingToHex(int *string,int len)
{
   int newHex[512];
   int i=0,j=0;
   for (i=0,j=0;i<len;i+=2,j++)
     newHex[j] = string[i]<<4 | string[i+1];
   return (&newHex[0]);
}

char* DecodePackString(int *hex,int len)
{
   int i;
   char OriginalChar[512];
   printf("\n\nCombining Hex Values are: ");
   hex = PackingToHex(hex,len);
   for (i=0;i<len/2;i++)
     printf("%d ",hex[i]);
   printf("\n\nHex to Decimal Values are: ");
   hex = HexToDec(hex,len);
   for (i=0;i<len/2;i++)
     printf("%d ",hex[i]);
  len = len + len/8;
   for (i=0;i<len/2;i++)
     OriginalChar[i] = (char)hex[i];
   OriginalChar[i] = '\0';
   printf("\n\nOriginal sentence is: %s",OriginalChar);
}

int main(void)
{
   char string[512];
   int hex[512],i;
   printf("\nEnter 8-Bit Packed Form String: ");
   gets(string);
   printf("\n\nYou have Entered %s",string);
   printf("\n\nOringinal Hex: ");
   for (i=0;string[i]!='\0';i++)
   {
     if(string[i] >= '0' && string[i] < 'A')
       hex[i] = string[i] - '0';
     else if (string[i] >= 'A')
       hex[i] = string[i] - 'A' + 10;
   }

  int l=strlen(string);
  for (i=0;i<l;i++)
     printf("%d ",hex[i]);
   DecodePackString(hex,l);

}

In this code i am taking input as PDU format and giving an output as characters(ASCII Value)

The code works fine for around 33rd character. But Long messeage(more than 33 character) gives garbage value.

Suppose the message is, hellohellohellohellohellohellohellohellohellohellohellohello

PDU: (only the message not other attributes) E8329BFD4697D9EC37BACC66BFD16536FB8D2EB3D96F7499CD7EA3CB6CF61B5D66B3DFE8329BFD4697D9EC37BACC66BFD16536FB0D

when i provide the string as input to the following code. the output is like this.

hellohellohellohellohellohellohellmkfl[--ZhB&H $ H$@D"

So,what did i do when i got this type of output. i didn't knew that my algorithm is correct or not so manually with pen and paper i have decoded it to 33rd character and i found that the value i m getting as message is wrong. How this is even possible? So i put my message in online PDU converter and everything is fine..

i have no idea how to approach this kind of error. please help me. thanks in advance
 
Top