|aaaaaaab|bbbbbbcc|cccccddd|ddddeeee|eeefffff|ffgggggg|ghhhhhhh|
Which packs 8 characters (a - h) into 7 octets. To unpack the message we have to pick out each individual character and make it an 8-bit ASCII character. It helps if we reorganize the data to look at each character individually as follows.
/* copyright (c) 2010 Steve Hill - All rights reserved */
char*
decode_7bit_ascii( uint_8 *sms, uint_8 len, uint_8 startPat )
{
uint_8 buffLen = len + 1;
char *buff = malloc( sizeof(char) * buffLen );
memset( buff, 0, sizeof(char) * buffLen );
char *currChar = buff; // current char in buff
char *lastChar = buff + len; // last char in buff
uint_8 *curr = sms; // current byte being converted
uint_8 *next = curr + 1; // next byte
uint_8 currPat = startPat; // conversion pattern
while( currChar < lastChar )
{
switch( currPat )
{
case 0: // aaaaaaax xxxxxxxx
*currChar = ( *curr >> 1 ) & 0x7F;
break;
case 1: // xxxxxxxa aaaaaaxx
*currChar = (( *curr << 6 ) & 0x40 ) +
(( *next >> 2 ) & 0x3F );
break;
case 2: // xxxxxxaa aaaaaxxx
*currChar = (( *curr << 5 ) & 0x60 ) +
(( *next >> 3 ) & 0x1F );
break;
case 3: // xxxxxaaa aaaaxxxx
*currChar = (( *curr << 4 ) & 0x70 ) +
(( *next >> 4 ) & 0x0F );
break;
case 4: // xxxxaaaa aaaxxxxx
*currChar = (( *curr << 3 ) & 0x78 ) +
(( *next >> 5 ) & 0x07 );
break;
case 5: // xxxaaaaa aaxxxxxx
*currChar = (( *curr << 2 ) & 0x7C ) +
(( *next >> 6 ) & 0x03 );
break;
case 6: // xxaaaaaa axxxxxxx
*currChar = (( *curr << 1 ) & 0x7E ) +
(( *next >> 7 ) & 0x01 );
break;
case 7: // xaaaaaaa xxxxxxxx
*currChar = *curr & 0x7F;
break;
}
currChar++;
if( currPat ) // stay on current byte if pattern 0
{
curr++;
next++;
}
currPat = ++currPat % 8;
}
return buff;
}