UD代码的实现例子:
void CPdu_formatDlg::gb2unicode(BYTE* src, BYTE* result)
{
// find unicode code based on gb code
CFile file;
CString strPath = GetDeviceFullPath();
CString sBinFilename = strPath + "gb2unicode.bin";
if(!file.Open(sBinFilename, CFile::modeRead)) {
AfxMessageBox("File can NOT be openned!");
return;
}
// calc gb index
UINT index = (src[0]<<8) + src[1];
// half-lookup method
int low,high,mid;
low = 0; high = MAX_UNI_INDEX;
UINT gbcode_mid = 0;
BYTE tmp[2];
while(low {
mid = (low+high)/2;
file.Seek((mid<<2)+2, CFile::begin);
file.Read((char*)tmp, 2);
gbcode_mid = (tmp[0]<<8) + tmp[1];
if( index < gbcode_mid ) high--;
else if( index > gbcode_mid ) low++;
else break;
}
if(low>high) {
AfxMessageBox("Cannot find gb char in current gb2unicode table!");
return;
}
// result
file.Seek( (mid<<2), CFile::begin );
file.Read((char*)tmp, 2);
result[0] = tmp[0];
result[1] = tmp[1];
file.Close();
}
void CPdu_formatDlg::OnButtonBuild()
{
char phone[32];
memset(phone, 0, sizeof(phone));
// (1) receive phone number inputed
m_editPhone.GetWindowText(phone, sizeof(phone));
if(strlen(phone) != 11) {
AfxMessageBox("Please input the correct mobile phone number!");
return;
}
for(int i=0; i<11; i++) {
if( (phone<'0') || (phone>'9') ) {
AfxMessageBox("Please input the correct mobile phone number!");
return;
}
}
// (2) receive sms text inputed
BYTE buf[256];
memset(buf, 0, sizeof(buf));
m_editInput.GetWindowText((char*)buf, sizeof(buf));
if(strlen((char*)buf) == 0) {
AfxMessageBox("Input should not be null!");
return;
}
// (3) build UD(sms text)
BYTE result[256];
memset(result, 0, sizeof(result));
UINT j=0, len1=0;
while(j < strlen((char*)buf)) {
// arcII
if(buf[j] < 128) {
result[len1] = 0x00;
result[len1+1] = buf[j];
j++;
len1 = len1+2;
}
// gb
else {
BYTE tmp[2];
gb2unicode((BYTE*)(&buf[j]), (BYTE *)tmp);
result[len1] = tmp[0];
result[len1+1] = tmp[1];
j++;
j++;
len1 = len1 + 2;
}
}
BYTE ud[256];
memset(ud, 0, sizeof(ud));
for(UINT k=0; k if( (result[k]>>4) < 10 ) { ud[(k<<1)] = (result[k]>>4) + '0'; }
else { ud[(k<<1)] = (result[k]>>4) - 10 + 'A'; }
if( (result[k]&0x0f) < 10 ) { ud[(k<<1)+1] = (result[k]&0x0f) + '0'; }
else { ud[(k<<1)+1] = (result[k]&0x0f) - 10 + 'A'; }
}
// (4) build DA(phone num)
BYTE da[256];
memset(da, 0, sizeof(da));
da[0] = '0'; // da_len
da[1] = 'D'; // da_len
da[2] = '9'; // type
da[3] = '1'; // type
da[4] = '6'; // china code
da[5] = '8'; // china code
da[6] = phone[1];
da[7] = phone[0];
da[8] = phone[3];
da[9] = phone[2];
da[10] = phone[5];
da[11] = phone[4];
da[12] = phone[7];
da[13] = phone[6];
da[14] = phone[9];
da[15] = phone[8];
da[16] = 'F';
da[17] = phone[10];
// (5) output pdu format after receiving >[sp]
BYTE pdu[256];
memset(pdu, 0, sizeof(pdu));
pdu[0] = '0'; // SCA (with embeded csca)
pdu[1] = '0'; // SCA (with embeded csca)
pdu[2] = '1'; // Type
pdu[3] = '1'; // Type
pdu[4] = '0'; // MR
pdu[5] = '0'; // MR
strcat((char*)pdu, (char *)da); // DA
pdu[24] = '0'; // PID
pdu[25] = '0'; // PID
pdu[26] = '0'; // DCS
pdu[27] = '8'; // DCS
pdu[28] = '0'; // VP
pdu[29] = '1'; // VP
if( (len1>>4) < 10 ) { pdu[30] = (len1>>4) + '0'; } // UDL
else { pdu[30] = (len1>>4) - 10 + 'A'; }
if( (len1&0x0f) < 10 ) { pdu[31] = (len1&0x0f) + '0'; } // UDL
else { pdu[31] = (len1&0x0f) - 10 + 'A'; }
memcpy(&pdu[32], ud, (len1<<1)); // ud
m_editOutput.SetWindowText((char *)pdu);
// (6) output the cmgs len
BYTE sLen[4];
memset(sLen, 0, sizeof(sLen));
len1 += 15;
sLen[0] = (len1/100) + '0';
sLen[1] = (len1%100)/10 + '0';
sLen[2] = (len1%10) + '0';
m_editLen.SetWindowText((char*)sLen);
3. 手机上成功收取并显示中文短消息.
Allen
2010.12.31 发表于<电子工程专辑>
构建PDU格式的中文SMS(I)
构建PDU格式的中文SMS(II)
用户1028144 2011-1-12 11:42
用户1028144 2011-1-12 11:41
用户1398709 2011-1-12 09:05
用户1589982 2011-1-11 14:36
用户1291225 2011-1-5 08:49
谢谢博主分享,下载学习
用户1190942 2011-1-4 08:22
用户1277994 2010-12-31 15:02
Thank you!