www.gusucode.com > VC写的短信串口发送程序源码程序 > VC写的短信串口发送程序源码程序/SMSDemo/SMSPDU.cpp
// PDU.cpp: implementation of the CSMSPDU class. // ////////////////////////////////////////////////////////////////////// #include "stdafx.h" #include "SMSDemo.h" #include "SMSPDU.h" #ifdef _DEBUG #undef THIS_FILE static char THIS_FILE[]=__FILE__; #define new DEBUG_NEW #endif ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// CSMSPDU::CSMSPDU() { } CSMSPDU::~CSMSPDU() { } size_t CSMSPDU::GetPDUContent(CString & strStream, LPCTSTR szCallCenter, LPCTSTR szTarget, LPCTSTR szContent) { strStream = GetEncAddr(szCallCenter, 0); strStream += conSendSeg; strStream += GetEncAddr(szTarget, 1); strStream += conUCodeSeg; strStream += GetUCSSegment(szContent); size_t nLen = GetStreamLength(strStream); return nLen; } //将Unicode编码还原为文字string CString CSMSPDU::ConvertUnicode2String(CString strDataSrc) { CString objStr; char * szContent = new char[400]; memset(szContent, 0, 400); strDataSrc = ConvertOctet(strDataSrc); for (int i=0, j=0; i < strDataSrc.GetLength(); i+=2,j++) { CString objByte = strDataSrc.Mid(i, 2); unsigned short intByteCode = HexString2Int(objByte); szContent[j] = (unsigned char)intByteCode; } BSTR bstrText = (BSTR)szContent; char szText[400] = {0}; WideCharToMultiByte(CP_ACP, 0, bstrText, SysStringLen(bstrText), szText, 400, NULL, NULL); objStr = szText; delete szContent; return objStr; } //交换半个字节,如13925151775F转换后为3129151577F5 CString CSMSPDU::ConvertSemiOctet(LPCTSTR lpStr) { CString objStr(lpStr); int i, nLen; TCHAR chTemp1, chTemp2; nLen = objStr.GetLength(); for(i = 0; i < nLen; i += 2) { chTemp1 = objStr.GetAt(i); chTemp2 = objStr.GetAt(i + 1); objStr.SetAt(i, chTemp2); objStr.SetAt(i + 1, chTemp1); } return objStr; } //交换字节,如EF53E54E交换后为53EF4EE5 CString CSMSPDU::ConvertOctet(LPCTSTR lpStr) { CString objStr(lpStr); int i, nLen; TCHAR chTemp1, chTemp2, chTemp3, chTemp4; nLen = objStr.GetLength(); for(i = 0; i < nLen; i += 4) { chTemp1 = objStr.GetAt(i); chTemp2 = objStr.GetAt(i + 1); chTemp3 = objStr.GetAt(i + 2); chTemp4 = objStr.GetAt(i + 3); objStr.SetAt(i, chTemp3); objStr.SetAt(i + 1, chTemp4); objStr.SetAt(i + 2, chTemp1); objStr.SetAt(i + 3, chTemp2); } return objStr; } //将地址进行编码,返回编码后的地址串 //nType表示地址类型,0表示地址为消息中心,1表示目的地址 CString CSMSPDU::GetEncAddr(LPCTSTR bStr, int nType) { CString objStr(bStr); CString objTemp; int nLen; CString objLen; nLen = objStr.GetLength(); //如果最左边是"+"号,则去掉它 if(objStr.Left(1) == (LPCTSTR)conADText) { objTemp = objStr; objStr = objTemp.Right(objTemp.GetLength() - 1); } //如果长度不为偶数,则加上"F"号 if((objStr.GetLength() % 2) != 0) { objStr += conPostfix; } objTemp = objStr; objStr = ConvertSemiOctet((LPCTSTR)objTemp); objStr = (LPCTSTR)conADType + objStr; switch(nType) { case 0: nLen = objStr.GetLength() / 2; break; case 1: break; default: break; } objLen.Format(_T("%02X"), nLen); objStr = objLen + objStr; return objStr; } //获取内容段 //传入内容字符,转换成Unicode,再获取总长度 CString CSMSPDU::GetUCSSegment(LPCTSTR bstrContent) { CString objTemp(bstrContent); CString objStr; CString objChar; BYTE * achBuffer = (BYTE *)(LPCTSTR)objTemp; int nLen = objTemp.GetLength(); BSTR wszBuffer = (CString(achBuffer)).AllocSysString(); BYTE abyte[3] = {0}; nLen = wcslen(wszBuffer); for(int i = 0; i < nLen; i++) { objChar.Format(_T("%04X"), wszBuffer[i]); objStr += objChar; } objChar.Format(_T("%02X"), nLen * 2); objStr = objChar + objStr; return objStr; } //获取字符流的长度,不包括短消息中心的长度 unsigned int CSMSPDU::GetStreamLength(CString objStream) { unsigned int nLen, nCenterLen; nLen = objStream.GetLength() / 2; CString objTemp = objStream.Left(2); const TCHAR* lpBuffer = (LPCTSTR)objTemp; TCHAR* strStop; nCenterLen = (unsigned int)_tcstoul(lpBuffer, &strStop, 10); nCenterLen ++; nLen -= nCenterLen; return nLen; } //获取abyte字符串的第一个字,并将其值转为数字 //用于解码时将十六进制的字符串转为数字 int CSMSPDU::HexString2Int(LPCTSTR abyte) { int iResult; if (abyte[0] >= 65 && abyte[0] <= 70) { iResult = (abyte[0] - 55) * 16; } else if (abyte[0] >=97 && abyte[0] <= 102) { iResult = (abyte[0] - 87) * 16; } else if (abyte[0] >=48 && abyte[0] <=57) { iResult = (abyte[0] - 48) * 16; } else return(-1); if (abyte[1] >= 65 && abyte[1] <= 70) { iResult += abyte[1] - 55; } else if (abyte[1] >=97 && abyte[1] <= 102) { iResult += abyte[1] - 87; } else if (abyte[1] >=48 && abyte[1] <=57) { iResult += abyte[1] - 48; } else return(-1); return(iResult); } //获取abyte字符串的第一个字,并将其值转为数字 //用于解码时将十进制的字符串转为数字 int CSMSPDU::OctString2Int(LPCTSTR abyte) { int iResult; if (abyte[0] >=48 && abyte[0] <=57) { iResult = (abyte[0] - 48) * 10; } else return(-1); if (abyte[1] >=48 && abyte[1] <=57) { iResult += abyte[1] - 48; } else return(-1); return(iResult); }