How to Pack GSM-7 Characters Into Septets

gsm 7 characters

Once we have our text in the GSM-7 character set, we’re ready to write the septets. As is show before, the mapping is kind of awkward, see 3GPP TS 23.038.

Here is the algorithm I use to achieve this:

[php]

/*
GSM-7 packing routing.
Written by Jeroen @ Mobile Tidings (http://mobiletidings.com)
*/
int /* Returns -1 for success, 0 for failure */
SMS_GSMEncode(
int inSize, /* Number of GSM-7 characters */
char* inText, /* Pointer to the GSM-7 characters. Note: I could
not have used a 0-terminated string, since 0
represents ‘@’ in the GSM-7 character set */
int paddingBits, /* If you use a UDH, you may have to add padding
bits to properly align the GSM-7 septets */
int outSize, /* The number of octets we have available to write */
unsigned char* outBuff, /* A pointer to the available octets */
int *outUsed /* Keeps track of howmany octets actually were used */
)
{
int bits = 0;
int i;
unsigned char octet;
*outUsed = 0;
if( paddingBits )
{
bits = 7 – paddingBits;
*outBuff++ = inText[0] << (7 – bits);
(*outUsed) ++;
bits++;
}
for( i = 0; i < inSize; i++ )
{
if( bits == 7 )
{
bits = 0;
continue;
}
if( *outUsed == outSize )
return 0; /* buffer overflow */
octet = (inText[i] & 0x7f) >> bits;
if( i < inSize – 1 )
octet |= inText[i + 1] << (7 – bits);
*outBuff++ = octet;
(*outUsed)++;
bits++;
}
return -1; /* ok */
}

[/php]

The padding bits are used to make sure the GSM-7 septets are written on a septet boundary. If you don’t use a User Data Header (UDH) for combining SMS messages or EMS text formatting or something else and your text starts at the first octet of the User Data (UD), you can leave out padding (set paddingBits to 0).

If you have a UDH than the paddingBits can be calculated as follows:

[php]

paddingBits = ((UDHL + 1 ) * 8 ) % 7;
if( paddingBits ) paddingBits = 7 – paddingBits;

[/php]

UDHL stands for User Data Header Length. I hope this helps everybody who is struggling with GSM-7 encodings.

Total
0
Shares
Related Posts
>