有效地编码和解码二进制格式的可变长度整数

时间:2016-03-30 17:47:23

标签: optimization encoding binary decoding

我需要在二进制文件中存储范围(0-50000000)中的整数,并在以后解码它们。为了节省空间,我存储了在第一个字节的前2位中解码整数所需的字节数,即01XXXXXX指的是保存数字需要2个字节。我在实现中遇到问题。解码后我得到的数字不是正确。 这是我的示例代码 -

int main()
{
    FILE *input = NULL,  
            *output = NULL;

    output = fopen("sample.bin","wb+");

    unsigned int num =  32594; //(this number would come from input file)
    char buff_ts[4];
    sprintf(buff_ts,"%d",num);
    setBitAt(buff_ts, sizeof(buff_ts), 23, 1); // set first two bits
    fwrite(buff_ts,1,sizeof(buff_ts),output);
    fclose(output);

    input = fopen("sample.bin", "rb");

    int diff;
    char buff[1];
    fread(buff,1,1,input);
    char buff_copy = buff[0];
    int temp = atoi(buff);

    int more_bytes_to_read = (temp>>6); // read first 2 bits
    buff_copy = buff_copy & ((1<<6)-1); // reset first 2 bits

    if(more_bytes_to_read==0) // if no more bytes to read
    {
        diff = buff_copy;
    }
    else
    {
        char extra_buff[more_bytes_to_read];
        fread(extra_buff,1,sizeof(extra_buff),input); // read extra bytes
        char num_buf[more_bytes_to_read+1];
        num_buf[0] = buff_copy;  // copy prev read buffer
        for(int i=1;i<=more_bytes_to_read;i++)
        {
            num_buf[i] = extra_buff[i-1];
        }
        diff = atoi(num_buf);
    }
        cout<<diff<<endl;

        return 0;
}

1 个答案:

答案 0 :(得分:2)

$digest