{"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

\/*
\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

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;
\nif( paddingBits ) paddingBits = 7 – paddingBits;<\/p>\n[\/php]\n

UDHL stands for User Data Header Length. I hope this helps everybody who is struggling with GSM-7 encodings<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"Once we have our text in the GSM-7 character set, we\u2019re ready to write the septets. As is…\n","protected":false},"author":2,"featured_media":844,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[29],"tags":[],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"https:\/\/techsofar.com\/wp-content\/uploads\/2020\/03\/voicemail-indication.jpg","_links":{"self":[{"href":"https:\/\/techsofar.com\/wp-json\/wp\/v2\/posts\/834"}],"collection":[{"href":"https:\/\/techsofar.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techsofar.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techsofar.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/techsofar.com\/wp-json\/wp\/v2\/comments?post=834"}],"version-history":[{"count":0,"href":"https:\/\/techsofar.com\/wp-json\/wp\/v2\/posts\/834\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techsofar.com\/wp-json\/wp\/v2\/media\/844"}],"wp:attachment":[{"href":"https:\/\/techsofar.com\/wp-json\/wp\/v2\/media?parent=834"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techsofar.com\/wp-json\/wp\/v2\/categories?post=834"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techsofar.com\/wp-json\/wp\/v2\/tags?post=834"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}