Click here to Skip to main content
15,881,413 members
Articles / Programming Languages / C#
Tip/Trick

Encoding / Decoding 7 bit User Data for SMS PDU (PDU Bit Packer)

Rate me:
Please Sign up or sign in to vote.
5.00/5 (10 votes)
10 Oct 2012CPOL3 min read 186.7K   10.7K   15   17
A library for packing / unpacking 7bit user data for SMS according to the GSM 03.38 standards.

Introduction

Most developers who develop applications that send SMS will eventually hit the problem of encoding SMS text into 7bit packed bytes. I found a lot of approaches and implementations that attempt to solve this issue on the web, but the ones that actually worked were either buggy or had serious performance issues.

The SMS application that I was developing was designed to send and receive millions of SMS daily, so what I needed was a simple, easy to use and high performance 7bit PDU packing and unpacking library. I developed the 7 bit PDU packing library and thought to share it with other developers, so they can invest their precious time somewhere else like improving their applications, solving other issues or just having some good time with family or friends.  

Packing and Unpacking

The most common text encodings for SMS text (user data) are GSM encoding (7bits) and Unicode (16bits). GSM encoding is commonly used for Latin (English, German, Spanish, …) text messages where each character is represented using 7bits only. The GSM encoding can map 128 Latin characters.

The user data part of a SMS holds the encoded text data. According to the GSM 03.38, the user data can hold up to 140 bytes. When using the GSM encoding, the 7bits characters must be packed into 8bits bytes. This allows the SMS to hold 160 Latin characters in its user data field.

The 7 bits binary representation of a character is called a septet and the 8bit binary representation is called an octet.

The process of filling septets (7bits characters) into octets (8bits bytes) is called packing. The reverse process is called unpacking which means extracting septets from the packed data.

The Packing Protocol 

The best way to understand how to pack septets into octets is by example. The following steps will describe how to pack the text ‘12345678’ according to the GSM encoding:

Step One: Convert the text to septets according to the GSM characters table:

Image 1

Step Two: Move the least significant bits from the next septet to the current one to create octets:

Image 2

The final result will look like this:

Image 3

Step Three: Complete the last byte's bits to 8 bits by padding it with zeros.

The unpacking process is exactly the reverse steps of the packing process.

Using The Library

The library is wrapped in the PduBitPacker class. The methods the library contains are:

  • PackBytes : Packs an unpacked 7 bit array to an 8 bit packed array according to the GSM protocol. This method has 3 overloads.
  • UnpackBytes : Unpacks a packed 8 bit array to a 7 bit unpacked array according to the GSM protocol.
  • ConvertHexToBytes :This is a utility method that converts a hex string to a byte array.
  • ConvertBytesToHex : This is a utility method that converts a byte array to a hex string.
Image 4

An example of packing:

C#
// Filling the array with demo data to be packed
// The byte array is the GSM default encoding character codes for the following text:
// "12345678"
byte[] unpackedBytes = { 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38 };
// Pack the bytes
byte[] packedBytes = PduBitPacker.PackBytes(unpackedBytes);
// Display the output as a hex string
MessageBox.Show(PduBitPacker.ConvertBytesToHex(packedBytes));
Image 5

An example of unpacking bytes:  

C#
// Fill the array with the packed bytes
byte[] packedBytes = PduBitPacker.ConvertHexToBytes("31D98C56B3DD70");
// Unpack the bytes
byte[] unpackedBytes = PduBitPacker.UnpackBytes(packedBytes);
// Display the output as a hex string
MessageBox.Show(PduBitPacker.ConvertBytesToHex(unpackedBytes));
Image 6

References

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Syrian Arab Republic Syrian Arab Republic
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionHow we will do if we have more the 8 Byte ? Pin
Sagar Saini 202325-Dec-23 20:07
Sagar Saini 202325-Dec-23 20:07 
QuestionBug in UnpackBytes Pin
alexplev_13-Dec-16 21:00
alexplev_13-Dec-16 21:00 
QuestionThanks! Simple algorithm. Problem with ex characters. Pin
don-and-home6-Jul-14 22:19
don-and-home6-Jul-14 22:19 
AnswerRe: Thanks! Simple algorithm. Problem with ex characters. Pin
hadilab3-Aug-14 0:20
hadilab3-Aug-14 0:20 
QuestionInvalid .zip Pin
thisisthemurph17-May-14 0:35
thisisthemurph17-May-14 0:35 
AnswerRe: Invalid .zip Pin
Member 1069018927-May-14 0:18
Member 1069018927-May-14 0:18 
QuestionProblem with @ and underScore ( _ ) in PDU CODE Pin
Member 106901894-Apr-14 0:20
Member 106901894-Apr-14 0:20 
AnswerRe: Problem with @ and underScore ( _ ) in PDU CODE Pin
hadilab3-Aug-14 1:24
hadilab3-Aug-14 1:24 
Questionkeeping user data header into account packing/unpacking Pin
Member 97577541-Oct-13 4:04
professionalMember 97577541-Oct-13 4:04 
GeneralThank Pin
_Ares!!!29-Jan-13 12:35
_Ares!!!29-Jan-13 12:35 
QuestionConverted to C++ Pin
Mark Beckwith5-Dec-12 7:14
Mark Beckwith5-Dec-12 7:14 
AnswerRe: Converted to C++ Pin
Member 1008063528-May-13 4:02
Member 1008063528-May-13 4:02 
BugOverflow exception Pin
ronnyr9-Oct-12 23:02
ronnyr9-Oct-12 23:02 
GeneralRe: Overflow exception Pin
Lama Barri10-Oct-12 5:24
Lama Barri10-Oct-12 5:24 
GeneralMy vote of 5 Pin
hadilab6-Oct-12 0:02
hadilab6-Oct-12 0:02 
GeneralRe: My vote of 5 Pin
Member 1069018927-May-14 0:19
Member 1069018927-May-14 0:19 
GeneralRe: My vote of 5 Pin
hadilab3-Aug-14 11:00
hadilab3-Aug-14 11:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.