我有一个数据集,由一个掩码文件和一个数据文件组成。掩码文件告诉解码器对于数据文件的当前偏移量,每个字段是否存在 8 位或 4 位。我需要根据掩码将数据移出,并编写解码文件,每个字段全部 8 位。我正在尝试在 C 中完成此操作。
void shift_4bits_left(unsigned char* array, unsigned short size)
{
int i;
unsigned char shifted = 0x00;
unsigned char overflow = (0xF0 & array[0]) >> 4;
for (i = (size - 1); i >= 0; i--)
{
shifted = (array[i] << 4) | overflow;
overflow = (0xF0 & array[i]) >> 4;
array[i] = shifted;
}
}
在功能上:
while(len>count){
//count=0;
if(bit==0)yy=fread(blockchar,1,len,fp1);
if(bit==0)memcpy(blockchar2,blockchar,len);
count+=yy;
memset(outputbuf,0,64);
for(x=0;x<len;x++){
count2=0;
//count2=1;
if(sometests(blockchar[x])==1)
{
shift_4bits_left(&blockchar[x],(yy-(bittest/2))+1);
count2=1;
total++;
if(total%2==0)len--;
}
//set bit for current position in mask file's buffer
if((x)%8 == 0)outputbuf[x]+=(count2<<7);
if((x)%8 == 1)outputbuf[x]+=(count2<<6);
if((x)%8 == 2)outputbuf[x]+=(count2<<5);
if((x)%8 == 3)outputbuf[x]+=(count2<<4);
if((x)%8 == 4)outputbuf[x]+=(count2<<3);
if((x)%8 == 5)outputbuf[x]+=(count2<<2);
if((x)%8 == 6)outputbuf[x]+=(count2<<1);
if((x)%8 == 7)outputbuf[x]+=count2;
}
问题是在处理大型数据集时这非常慢,我宁愿对文件进行迭代,而不将文件存储在数组中。