#include <stdio.h> int gsmEnode7bit(unsigned char* pDst,const char* pSrc) { int nSrc; // 源字符串的计数值 int nDst; // 目的字符串的计数值 int nChar; // 当前正在处理的组内字符的字节序号 unsigned char nLeft; //上一字节残留的数据 int nSrc_length = strlen(pSrc); //计数初始化 nSrc = 0; nDst = 0; //将源串的每8个字节为一组,压缩为7个字节 while(nSrc < nSrc_length) { //取源字符串的低三位 nChar = nSrc & 7; //处理源字符串的每个字节 if(0 == nChar) { //组内第一个字节, nLeft = * pSrc; } else { *pDst = (*pSrc << (8 - nChar)) | nLeft; nLeft = *pSrc >> nChar; //修改目标串的指针和计数值 pDst++; nDst++; } pSrc++; nSrc++; } return nDst; }
<pre name="code" class="cpp">int gsmDecode7bit(char * pDst,const char *pSrc) { int nSrc; int nDst; int nByte; unsigned char nLeft; nSrc_leng = strlen(pSrc); nSrc = 0; nDst = 0; nByte = 0; nLeft = 0; while(nSrc < nSrc_leng) { *pDst = (((unsigned char)(*pSrc)<< nByte) | nLeft) & 0x7f; nLeft = (unsigned char)(*pSrc)>>(7-nByte); pDst++; nDst++; nByte++; if(nByte == 7) { *pDst = nLeft; pDst++; nDst++; nByte = 0; nLeft = 0; } pSrc++; nSrc++; } *Dst = 0; return nDst; } int main() { char name[20] = "hello world"; char nn[20]; gsmEnode7bit(nn,name); gsmDecode7bit(name,nn); printf("%s\n",name); }
原文地址:http://blog.csdn.net/zhangxxxww/article/details/44062095