将数据按X位移入和移出文件的最佳方法是什么? [英] What is the best way to shift data by X bits into and out of a file?

查看:50
本文介绍了将数据按X位移入和移出文件的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据集,由一个掩码文件和一个数据文件组成.掩码文件告诉解码器,对于数据文件的当前偏移量,每个字段是否存在8位或4位.我需要根据掩码将数据移出,并写入解码的文件,每个字段所有8位.我正在尝试用C语言实现这一目标.

I have a dataset, made up of a mask file, and a data file. the mask file tells the decoder whether there are 8 bits per field present, or 4, for the current offset of the datafile. I need to shift the data out according to the mask, and write the decoded file, with all 8 bits per field. I'm trying to accomplish this in 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;
        }

问题是,使用大型数据集时,这非常慢,我希望对文件进行迭代,而无需将文件存储在数组中.

The problem is this is very slow when working with large datasets, I would prefer to make this iterative over the file, without storing the files in arrays.

推荐答案

我通过使用memcpy一次查看了256个字节(不移位时)来解决了这个问题.感谢您的帮助.

I solved it by looking at 256 bytes at a time, and when not shifting, using memcpy. Thanks for the help guys.

这篇关于将数据按X位移入和移出文件的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆