# 数据的压缩存储与解压缩算法实现(C语言)

/*
table[0] = tab[0] & 0x0f ;  //取低位
table[1] = tab[0] & 0xf0 ； //取高位
table[2] = tab[1] & 0x0f ;  //取低位
table[3] = tab[1] & 0xf0 ;	//取高位
...
table[6] = tab[3] & 0x0f ;  //取低位
table[7] = tab[3] & 0xf0 ;	//取高位

*/

#include <stdio.h>
unsigned char tab[4] = {0xfe,0xf1,0x3e,0x3c} ;
unsigned char table[8] ;
int main(void)
{
int i ;
int j ;
int count = 0 ;
for(i = 0 ; i < 8 ; i++)
{
if(i % 2 == 0)
table[i] = tab[count] & 0x0f ;
if(i % 2 != 0)
{
table[i] = tab[count] & 0xf0 ;
count++ ;
}
}
for(i = 0 ; i < 8 ; i++)
printf("table[%d]:0x%x\n",i,table[i]) ;
}

运行结果:
table[0] : 0xe
table[1] : 0xf0
table[2] : 0x1
table[3] : 0xf0
table[4] : 0xe
table[5] : 0x30
table[6] : 0xc
table[7] : 0x30 

/*
savebuf[0] = table[0] | table[1] ;
savebuf[1] = table[2] | table[3] ;
savebuf[2] = table[4] | table[5] ;
savebuf[3] = table[6] | table[7] ;
*/

#include <stdio.h>
unsigned char tab[4] = {0xfe,0xf1,0x3e,0x3c} ;
unsigned char table[8] ;
int savebuf[4] ;

int main(void)
{
int i ;
int j ;
int low , high , count = 0 , beat_count ,value ;
count = 0 ;
beat_count = 0 ;
for(j = 0 ; j < 8 ; j++)
{
if(j % 2 == 0)
low = table[j] ;
else
{
high = table[j] ;
count++ ;
savebuf[beat_count] = low | high ;
beat_count++;
}
}
for(i = 0 ; i < 4 ; i++)
printf("savebuf[%d]:%x\n",i , savebuf[i]) ;
} 

运行结果:
savebuf[0]:fe
savebuf[1]:f1
savebuf[2]:3e
savebuf[3]:3c

+ 订阅