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


 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值