Click here to Skip to main content
14,607,863 members

PDU lib under STL for SMS

Rate this:
3.22 (6 votes)
Please Sign up or sign in to vote.
3.22 (6 votes)
21 May 2007CPOL
This is a light weight lib for decoding and encoding the GSM SMS PDU format.


The SMS message, as specified by the Etsi organization (documents GSM 03.40 and GSM 03.38), can be up to 160 characters long, where each character is 7 bits according to the 7-bit default alphabet. Eight-bit messages (max 140 characters) are usually not viewable by the phones as text messages; instead, they are used for data in, e.g., smart messaging (images and ringing tones) and OTA provisioning of WAP settings. 16-bit messages (max 70 characters) are used for Unicode (UCS2) text messages, viewable by most phones. A 16-bit text message of class 0 will, on some phones, appear as a Flash SMS (a.k.a. blinking SMS or alert SMS).

Please see dreamfabric for details.

This lib is used to encode/decode plain text to PDU format under 7-bit or UCS2 (yes, the EMS is not supported yet). This lib is easy to use, but not bug free, so please be patient :).

I have searched the internet, and there are some great libs for the PDU format under the .NET Framework which I can't use in my current job, so I wrote this lib for my own needs.

Source code

The source code is very simple, only two files; you can add them to your project or build a lib.

How to use...

There are two main function for the lib: Compose() and Fetch(). Using these two functions, you can compose a message or fetch data form a PDU string.

 *    Compose a shot message
 *    @para pdudata pdu: [out] The pdu result
 *    @para string msg:    data need to be sent
 *    @para string phone:    the destination phone number
 *    @para string msc:    message center number, if null, use the preset number
 *    @para EEncodeMethod eMethod:    the encode method, default is UCS2
 *    @return the data length without the SMSC part[you need it in AT+CMGS command]
int Compose (pdudata& pdu, 
    std::tstring msg, std::tstring phone,
    std::tstring msc = _T(""),
    EEncodeMethod eMethod = EEncodeUCS2) throw(...);

 *    Fetch data form a pdu format data
 *    only return the message part(USERDATA), 
      use other member function to fetch information
 *    @para pdudata pdu: the source data
 *    @return The nomral message
std::tstring Fetch(pdudata pdu) throw(...);

Compose an SMS message

To compose a message, you need do this:

CPDU pdu;
LPCTSTR szMsg = m_szEncodeContents.GetBuffer(m_szEncodeContents.GetLength());
pdu.SetMSCNumber(_T("13800210500"));    // Set the message center number here
try {

    CPDU::pdudata strPDU;
    // Compose the message
    int iLen = pdu.Compose(strPDU, szMsg, _T("13813878775"));
    WTL::CString csPDU;

    // Show the message to the output edit control
#ifdef _UNICODE
    csPDU = strPDU.c_str();
#endif // _UNICODE

    m_szDecodeContents = csPDU;
} catch (CPDUException e) {
    m_szDecodeContents = e.GetMessage().c_str();

Fetch data

When you need to fetch a message, you need do this:

CPDU pdu;
WTL::CString szMessage = pdu.Fetch(pdustring);

If there are no exceptions, then you can use these functions to fetch other data:

 *    Fetch Data
// The caller/sender number
std::tstring GetCaller()        { return m_szCaller; }
// The callee/recevier number
std::tstring GetCallee()        { return m_szCallee; }
// Message time stamp
std::tstring GetTimeStamp()        { return m_szTimeStamp; }


The tstl.h file is a TCHAR style header file for STL strings and streams.


  • May 22 2007: Fixed a bug in the Decode7bit function, thanks to alexafros12345 :)


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


About the Author

Louis huang
China China
No Biography provided

Comments and Discussions

Generalwhere i can find stl/stl.h header file Pin
krshna.padave17-Apr-09 0:41
Memberkrshna.padave17-Apr-09 0:41 
GeneralRe: where i can find stl/stl.h header file Pin
Youngho Kim29-Jan-10 23:24
MemberYoungho Kim29-Jan-10 23:24 
Generalsource code for PDU to Text Converter in java Pin
snehalpatil271-Mar-09 18:45
Membersnehalpatil271-Mar-09 18:45 
GeneralA bug about offset in parsing reply number in Fetch() Pin
Allen.W5-Nov-07 18:36
MemberAllen.W5-Nov-07 18:36 
QuestionRe: A bug about offset in parsing reply number in Fetch() Pin
afriza4-Sep-08 22:39
Memberafriza4-Sep-08 22:39 
GeneralCPDU::Encode7bit Fix [modified] Pin
waelahmed9-Oct-07 23:08
Memberwaelahmed9-Oct-07 23:08 
GeneralRe: CPDU::Encode7bit Fix Pin
patiqs11-Sep-08 23:41
Memberpatiqs11-Sep-08 23:41 
QuestionPossible to integrate with C# Pin
nonintrusive24-Jul-07 7:07
Membernonintrusive24-Jul-07 7:07 
Questionhow to send picture in SMS? Pin
Shakeel Mumtaz10-Jun-07 5:46
MemberShakeel Mumtaz10-Jun-07 5:46 
GeneralCPDU::Decode7bit - bug fix Pin
alexafros1234517-May-07 10:38
Memberalexafros1234517-May-07 10:38 
GeneralRe: CPDU::Decode7bit - bug fix Pin
Louis huang21-May-07 17:11
MemberLouis huang21-May-07 17:11 
GeneralNot working for me Pin
trAndy29-Apr-07 13:58
MembertrAndy29-Apr-07 13:58 
GeneralRe: Not working for me Pin
Louis huang29-Apr-07 16:47
MemberLouis huang29-Apr-07 16:47 
GeneralRe: Not working for me Pin
trAndy29-Apr-07 19:56
MembertrAndy29-Apr-07 19:56 
GeneralRe: Not working for me Pin
Louis huang29-Apr-07 23:10
MemberLouis huang29-Apr-07 23:10 
GeneralUse a webservice instead Pin
xaml.net13-Jan-07 2:30
Memberxaml.net13-Jan-07 2:30 
GeneralCopy from another site Pin
_johan6-Dec-06 3:26
Member_johan6-Dec-06 3:26 
GeneralRe: Copy from another site Pin
Louis huang6-Dec-06 14:27
MemberLouis huang6-Dec-06 14:27 
QuestionQuestion about another method.... Pin
prcarp27-Nov-06 2:26
Memberprcarp27-Nov-06 2:26 
I am new to SMS messaging but need to use it for my current project. I was lost trying to decipher the PDU format and was about to give up. Then I realized that the command:


would give the SMS output in comma-separated text format that was much easier to read and parse. This command is available on Sony-Ericsson and Sierra Wireless Aircard modems (and assuming others as well).

Am I losing any information by using this method instead?
AnswerRe: Question about another method.... Pin
Louis huang27-Nov-06 2:40
MemberLouis huang27-Nov-06 2:40 
GeneralRe: Question about another method.... Pin
prcarp27-Nov-06 3:45
Memberprcarp27-Nov-06 3:45 
GeneralRe: Question about another method.... Pin
Louis huang27-Nov-06 4:17
MemberLouis huang27-Nov-06 4:17 
QuestionCan you tell me the use? Pin
Priyank Bolia26-Nov-06 23:40
MemberPriyank Bolia26-Nov-06 23:40 
AnswerRe: Can you tell me the use? Pin
Louis huang27-Nov-06 2:21
MemberLouis huang27-Nov-06 2:21 
GeneralRe: Can you tell me the use? Pin
Priyank Bolia28-Nov-06 19:48
MemberPriyank Bolia28-Nov-06 19:48 

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.

Posted 26 Nov 2006


27 bookmarked