Click here to Skip to main content
Click here to Skip to main content
Go to top

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

, 10 Oct 2012
Rate this:
Please Sign up or sign in to vote.
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:

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

The final result will look like this:

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.

An example of packing:

 // 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)); 

An example of unpacking bytes:  

 
 // 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));

References

License

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

Share

About the Author

Lama Barri

Syrian Arab Republic Syrian Arab Republic
No Biography provided

Comments and Discussions

 
QuestionThanks! Simple algorithm. Problem with ex characters. Pinmemberdon-and-home6-Jul-14 22:19 
AnswerRe: Thanks! Simple algorithm. Problem with ex characters. Pinmemberhadilab3-Aug-14 0:20 
QuestionInvalid .zip Pinmemberthisisthemurph17-May-14 0:35 
AnswerRe: Invalid .zip PinmemberMember 1069018927-May-14 0:18 
QuestionProblem with @ and underScore ( _ ) in PDU CODE [modified] PinmemberMember 106901894-Apr-14 0:20 
AnswerRe: Problem with @ and underScore ( _ ) in PDU CODE Pinmemberhadilab3-Aug-14 1:24 
Questionkeeping user data header into account packing/unpacking [modified] PinmemberMember 97577541-Oct-13 4:04 
GeneralThank Pinmember_Ares!!!29-Jan-13 12:35 
QuestionConverted to C++ PinmemberMark Beckwith5-Dec-12 7:14 
AnswerRe: Converted to C++ PinmemberMember 1008063528-May-13 4:02 
BugOverflow exception Pinmemberronnyr9-Oct-12 23:02 
GeneralRe: Overflow exception PinmemberLama Barri10-Oct-12 5:24 
GeneralMy vote of 5 Pinmemberhadilab6-Oct-12 0:02 
GeneralRe: My vote of 5 PinmemberMember 1069018927-May-14 0:19 
GeneralRe: My vote of 5 Pinmemberhadilab3-Aug-14 11:00 

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.140926.1 | Last Updated 10 Oct 2012
Article Copyright 2012 by Lama Barri
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid