通过ASCII码对GSM PDU 7bit短信编码
int8_t Gsm_Encode7bitFromAscii(char* pDest,uint16_t dSize, uint16_t* pLen,const char* pSrc,uint16_t sSize) { uint16_t nSrc; uint16_t nDst; uint16_t nChar; uint8_t nLeft; uint16_t nSrcLength=sSize; nSrc = 0; nDst = 0; while(nSrc<nSrcLength) { nChar = nSrc & 7; if(nChar == 0) { nLeft = *pSrc; } else { *pDest = (*pSrc << (8-nChar)) | nLeft; nLeft = *pSrc >> nChar; pDest++; nDst++; } pSrc++; nSrc++; } return nDst; }
GSM PDU 7bit短信解码:
/* *函数功能:GSM PDU 7bit短信解码 *输入参数:pDest:存放ASCII码的目的地址指针, dSize:存放ASCII码的目的地址大小 pSrc: PUD 7BIT 原地址 sSize:原地址大小 *返回参数:0:转换成功,-1:转换失败 *函数说明: */ int8_t Gsm_Decode7bitToASCII(char* pDest,uint16_t dSize, uint16_t* pLen,const char* pSrc,uint16_t sSize) { uint16_t nSrc,nDst; uint16_t nByte; uint8_t nLeft; uint16_t nSrcLength; uint8_t ui8Array[3]; uint8_t ui8Addr = 0; /*检测数据有效性*/ if((pDest == NULL)||(dSize == 0)||(pSrc == NULL)||(sSize == 0)||(pLen == NULL)) return -1; /*如果目的地址空间不足,则直接返回错误*/ if(dSize < (sSize*8/7+1)*2) return -1; nSrc = 0; nDst = 0; nByte = 0; nLeft = 0; nSrcLength=sSize; while(nSrc<nSrcLength) { ui8Array[ui8Addr] = (((unsigned char)(*pSrc) << nByte) | nLeft) & 0x7f; nLeft = (unsigned char)(*pSrc)>> (7-nByte); ui8Addr++; nDst++; nByte++; if(nByte == 7) { if(ui8Addr==2) { ui8Addr = 0; *pDest++ = AsciiToHex(ui8Array[0],ui8Array[1]); } else if(ui8Addr>2) { return -1; } ui8Array[ui8Addr] = nLeft; ui8Addr++; nDst++; nByte = 0; nLeft = 0; } if(ui8Addr==2) { ui8Addr = 0; *pDest++ = AsciiToHex(ui8Array[0],ui8Array[1]); } else if(ui8Addr>2) { return -1; } pSrc++; nSrc++; } *pLen = nDst; return 0; }