Introduction
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.
int Compose (pdudata& pdu,
std::tstring msg, std::tstring phone,
std::tstring msc = _T(""),
EEncodeMethod eMethod = EEncodeUCS2) throw(...);
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")); try {
CPDU::pdudata strPDU;
int iLen = pdu.Compose(strPDU, szMsg, _T("13813878775"));
WTL::CString csPDU;
#ifdef _UNICODE
MultiByteToWideChar(
CP_ACP,
MB_PRECOMPOSED,
strPDU.c_str(),
-1,
csPDU.GetBuffer(1024),
1024
);
#else
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:
std::tstring GetCaller() { return m_szCaller; }
std::tstring GetCallee() { return m_szCallee; }
std::tstring GetTimeStamp() { return m_szTimeStamp; }
Notice
The tstl.h file is a TCHAR
style header file for STL strings and streams.
Update
- May 22 2007: Fixed a bug in the
Decode7bit
function, thanks to alexafros12345 :)