GSM 7 bit encoding and decoding in C

73 Views Asked by At

I wanted to ask how to encode and decode a string like "*123*1#" using gsm 7 bit encoding in c.

I tried to write the code and it was working perfectly but the string "*123*1#" gives the last value as 00 and that decodes to @ according to gsm 7 bit charset.

int gsm7encoding(const char* input, unsigned char* output, int* outlen)
{
unsigned bit_count = 0;
unsigned bit_queue = 0;
   unsigned char *local=NULL;
   local=output;
   int inlen = 0;
(*outlen) = 0;
    int lOutlen=0;
while (*input) {
   
  bit_queue |= (*input & 0x7Fu) << bit_count;
  bit_count += 7;

  if (bit_count >= 8) {
    *output++ = (unsigned char) bit_queue;
    (*outlen)++;
     lOutlen++;
    bit_count -= 8;
    bit_queue >>= 8;
  }
  input++;
        inlen++;
}
if (bit_count > 0) {
  *output++ = (uint8_t) bit_queue;
    (*outlen)++;
     lOutlen++;
  }
   

printf("inlen (%d) *outlen(%d)\n",inlen, *outlen);
printf("inlen (%d) lOutlen(%d)\n",inlen, lOutlen);

return 1;
}
0

There are 0 best solutions below