码迷,mamicode.com
首页 > 其他好文 > 详细

GSM压缩/ 解压实现

时间:2015-03-04 16:56:03      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:c   gsm   

#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);  
}  




GSM压缩/ 解压实现

标签:c   gsm   

原文地址:http://blog.csdn.net/zhangxxxww/article/details/44062095

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!