手机短信PDU编码与解码
来源:互联网 发布:eric 6 python 编辑:程序博客网 时间:2024/05/04 04:44
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- bool CMobile::EncodeSMS(CSMS sms, CPDUSMS & pdu)
- {
- try
- {
-
- CString sTo = sms.m_sNumber;
- CString sTA;
-
- sTo.TrimLeft("+");
-
- if(sTo.Left(2) == "86")
- {
- sTA = "91";
- }
- else if((sTo.GetLength() == 11) && sTo.Left(2) == "13")
- {
- sTo = "86" + sTo;
- sTA = "91";
- }
-
- else
- {
- sTA = "A1";
- }
-
-
- int nAL = sTo.GetLength();
-
- if(nAL % 2)
- sTo += "F";
-
-
- sTo = Swap2(sTo);
-
- CString sTPDA;
- sTPDA.Format("%02X%s%s", nAL, sTA, sTo);
-
-
-
-
- CString sSMS = String2HEX(sms.m_sBody);
-
-
- if(!sms.m_nClass)
- {
- pdu.m_sPDUBody.Format(_T("001100%s000801%02X%s"),
- sTPDA, sSMS.GetLength() / 2, sSMS);
-
- }
-
- else
- {
- pdu.m_sPDUBody.Format(_T("001100%s001801%02X%s"),
- sTPDA, sSMS.GetLength() / 2, sSMS);
- }
-
- pdu.m_sHeader.Format(_T("AT+CMGS=%03d"), pdu.m_sPDUBody.GetLength() / 2 - 1);
- }
- catch(CException * e)
- {
- e->Delete();
- return false;
- }
- return true;
- }
-
-
- bool CMobile::DecodeSMS(CPDUSMS pdu, CSMS & sms)
- {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- try
- {
- char cBuffer[512];
-
- strcpy(cBuffer, pdu.m_sHeader);
-
- int nLength;
- sscanf(cBuffer + 7, "%d,%d,,%d", &sms.m_nIndex, &sms.m_nType, &nLength);
-
-
-
- strcpy(cBuffer, pdu.m_sPDUBody);
-
-
- CString sc;
- sc = pdu.m_sPDUBody.Mid(6, 12);
- sc = Swap2(sc);
- sc.TrimRight("F");
-
- sms.m_sSC = sc;
-
-
- int nUserLength;
- int nBegin;
-
- CString sFormat;
-
- if(sms.m_nType != CMobile::smsUnsent)
- {
-
- sscanf(cBuffer + 20, "%02X", &nUserLength);
-
-
- if(nUserLength % 2)
- nUserLength ++;
-
- CString sNumber = pdu.m_sPDUBody.Mid(24, nUserLength);
- sNumber = Swap2(sNumber);
- sNumber.TrimRight("F");
- sNumber.TrimLeft("86");
-
- sms.m_sNumber = sNumber;
-
-
- nBegin = 24 + nUserLength + 2;
- sFormat = pdu.m_sPDUBody.Mid(nBegin, 2);
-
-
- nBegin += 2;
- CString sTime = pdu.m_sPDUBody.Mid(nBegin, 12);
- sTime = Swap2(sTime);
-
- int nYear, nMonth, nDay, nHour, nMinute, nSecond;
- sscanf(sTime, "%02d%02d%02d%02d%02d%2d", &nYear,
- &nMonth, &nDay, &nHour, &nMinute, &nSecond);
-
- CTime oTime(nYear + 2000, nMonth, nDay, nHour, nMinute, nSecond);
- sms.m_nTime = oTime.GetTime();
-
-
- nBegin += 14;
- }
-
-
- else
- {
- sms.m_sNumber = "(本地)";
- sms.m_nTime = 0;
-
-
- nBegin = 30;
- sFormat = pdu.m_sPDUBody.Mid(nBegin, 2);
-
- nBegin += 4;
- }
-
-
-
- sscanf(cBuffer + nBegin, "%02X", &nLength);
-
-
- nBegin += 2;
-
- int nBits = 16;
- if(sFormat == "00")
- nBits = 7;
- if(sFormat == "04")
- nBits = 8;
-
- sms.m_sBody = HEX2String(cBuffer + nBegin, nLength, nBits);
- }
- catch(CException * e)
- {
- e->Delete();
- return false;
- }
- return true;
- }
-
- CString CMobile::Swap2(CString sSource)
- {
- CString sSwap = sSource;
- for(int i = 0; i < sSource.GetLength(); i += 2)
- {
- sSwap.SetAt(i, sSource.GetAt(i + 1));
- sSwap.SetAt(i + 1, sSource.GetAt(i));
- }
- return sSwap;
- }
-
- CString CMobile::HEX2String(char *pSource, int nLength, int nBits)
- {
- CString sDecode;
- switch(nBits)
- {
-
- case 7:
- {
- unsigned char nch = 0;
- for(int i = 0; i < nLength; i++)
- {
- unsigned char ch;
- sscanf(pSource + i * 2, "%02X", &ch);
- sDecode += ((char)((unsigned char)(ch << (i % 7 + 1)) >> 1) + nch);
- nch = ((unsigned char)(ch >> (7 - i % 7)));
-
- if(i % 7 == 6)
- {
- sDecode += nch;
- }
- }
- break;
- }
- case 8:
- {
- for(int i = 0; i < nLength; i++)
- {
- unsigned char ch;
- sscanf(pSource + i * 2, "%02X", &ch);
- sDecode += ch;
- }
- break;
- }
-
- default:
- {
- wchar_t pwc[200];
-
- for(int i = 0; i < nLength / 2; i++)
- {
- sscanf(pSource + i * 4, "%04X", pwc + i);
- }
-
- pwc[i] = wchar_t('/0');
-
- _bstr_t bstrSMS(pwc);
- sDecode = (char *)bstrSMS;
- break;
- }
- }
-
- return sDecode;
- }
-
- CString CMobile::String2HEX(CString sSource)
- {
- _bstr_t bstrSMS(sSource);
- int nSMSLength = bstrSMS.length();
-
- wchar_t * pwc = (wchar_t *)bstrSMS;
-
- CString sEncode;
- for(int i = 0; i < nSMSLength; i++)
- {
- CString sHex;
- sHex.Format(_T("%04X"), pwc[i]);
- sEncode += sHex;
- }
-
- return sEncode;
- }