{"id":834,"date":"2020-03-08T12:01:25","date_gmt":"2020-03-08T12:01:25","guid":{"rendered":"https:\/\/mobiletidings.com\/?p=834"},"modified":"2020-09-16T20:31:17","modified_gmt":"2020-09-16T19:31:17","slug":"how-to-pack-gsm7-into-septets","status":"publish","type":"post","link":"https:\/\/techsofar.com\/how-to-pack-gsm7-into-septets\/","title":{"rendered":"How to Pack GSM-7 Characters Into Septets"},"content":{"rendered":"
Once we have our text in the GSM-7 character set, we\u2019re ready to write the septets. As is show\u00a0before<\/a>, the mapping is kind of awkward, see\u00a03GPP TS 23.038.<\/p>\n Here is the algorithm I use to achieve this:<\/p>\n[php]\n \/* The padding bits are used to make sure the GSM-7 septets are written on a septet boundary. If you don\u2019t use a User Data Header (UDH) for\u00a0combining SMS messages<\/a>\u00a0or\u00a0EMS text formatting<\/a>\u00a0or something else and your text starts at the first octet of the User Data (UD), you can leave out padding (set paddingBits to 0).<\/p>\n If you have a UDH than the paddingBits can be calculated as follows:<\/p>\n[php]\n paddingBits = ((UDHL + 1 ) * 8 ) % 7;
\nGSM-7 packing routing.
\nWritten by Jeroen @ Mobile Tidings (http:\/\/mobiletidings.com)
\n*\/
\nint \/* Returns -1 for success, 0 for failure *\/
\nSMS_GSMEncode(
\nint inSize, \/* Number of GSM-7 characters *\/
\nchar* inText, \/* Pointer to the GSM-7 characters. Note: I could
\nnot have used a 0-terminated string, since 0
\nrepresents ‘@’ in the GSM-7 character set *\/
\nint paddingBits, \/* If you use a UDH, you may have to add padding
\nbits to properly align the GSM-7 septets *\/
\nint outSize, \/* The number of octets we have available to write *\/
\nunsigned char* outBuff, \/* A pointer to the available octets *\/
\nint *outUsed \/* Keeps track of howmany octets actually were used *\/
\n)
\n{
\nint bits = 0;
\nint i;
\nunsigned char octet;
\n*outUsed = 0;
\nif( paddingBits )
\n{
\nbits = 7 – paddingBits;
\n*outBuff++ = inText[0] << (7 – bits);
\n(*outUsed) ++;
\nbits++;
\n}
\nfor( i = 0; i < inSize; i++ )
\n{
\nif( bits == 7 )
\n{
\nbits = 0;
\ncontinue;
\n}
\nif( *outUsed == outSize )
\nreturn 0; \/* buffer overflow *\/
\noctet = (inText[i] & 0x7f) >> bits;
\nif( i < inSize – 1 )
\noctet |= inText[i + 1] << (7 – bits);
\n*outBuff++ = octet;
\n(*outUsed)++;
\nbits++;
\n}
\nreturn -1; \/* ok *\/
\n}<\/p>\n[\/php]\n
\nif( paddingBits ) paddingBits = 7 – paddingBits;<\/p>\n[\/php]\n