【问题标题】:Why My DeCompression is showing an Error?为什么我的 DeCompression 显示错误?
【发布时间】:2014-10-15 13:11:41
【问题描述】:

此处还提供了代码,并且运行完美并显示错误 - http://ideone.com/gWEoIV

#include <stdio.h>
#include <stdlib.h>

unsigned char *MyCompress(unsigned int num, unsigned char *buffer);
unsigned int MyDeCompress(unsigned char **buffer);

int main(void) {
    unsigned char *abc, *abc2;
    unsigned len;
    abc=(unsigned char *)malloc(12);
    abc2=abc;
    len=25960;
    abc = MyCompress(len, abc);
    len=385;
    abc = MyCompress(len, abc);
    len=900;
    abc = MyCompress(len, abc);
    len=20560;
    abc = MyCompress(len, abc);
    len=384;
    abc = MyCompress(len, abc);
    abc=abc2;

    len = MyDeCompress(&abc);
    len = MyDeCompress(&abc);
    len = MyDeCompress(&abc);
    len = MyDeCompress(&abc);
    len = MyDeCompress(&abc);


    return 0;
}

unsigned int MyDeCompress(unsigned char **buffer){
    unsigned int c;
    unsigned int num = 0;
    unsigned char *p = *buffer;

    printf("\n\nDeCompression\n===============\n");

    do{
        c = ((unsigned char) *p++);
        printf("Read=%d\n", c);
        if (c <= 0) {
            printf("Error: c is < 0 in uncompress1()\n");
            exit(0);
        }
        num <<= 7;
        num |= c & 127;

        printf("uncompress: c = %d num = %d\n", c, num);

        if (!num)
            break;
    }while (c & 128);

    *buffer = p;

    printf("Returning %d\n", num);

    return num;
}


unsigned char *MyCompress(unsigned int num, unsigned char *buffer){
    int i = 0;
    unsigned int r = num;
    unsigned char temp;
    unsigned char s[5];

    printf("Compression\n===============\n");

    printf("received %d to compress\n", num);

    if(!r){
        *buffer++ = 0;
        return buffer;
    }

    while (r){
        s[i] = r & 127;
        r >>= 7;
        printf("s[%d]=%d; r=%d\n", i, s[i], r);
        i++;
    }

    while (--i >= 0){
        temp = (unsigned char)(s[i] | (i ? 128 : 0));
        printf("temp=%d\n", temp);
        *buffer++=temp;
    }
    return buffer;
}

//输出:

Compression
===============
received 25960 to compress
s[0]=104; r=202
s[1]=74; r=1
s[2]=1; r=0
temp=129
temp=202
temp=104
Compression
===============
received 385 to compress
s[0]=1; r=3
s[1]=3; r=0
temp=131
temp=1
Compression
===============
received 900 to compress
s[0]=4; r=7
s[1]=7; r=0
temp=135
temp=4
Compression
===============
received 20560 to compress
s[0]=80; r=160
s[1]=32; r=1
s[2]=1; r=0
temp=129
temp=160
temp=80
Compression
===============
received 384 to compress
s[0]=0; r=3
s[1]=3; r=0
temp=131
temp=0


DeCompression
===============
Read=129
uncompress: c = 129 num = 1
Read=202
uncompress: c = 202 num = 202
Read=104
uncompress: c = 104 num = 25960
Returning 25960


DeCompression
===============
Read=131
uncompress: c = 131 num = 3
Read=1
uncompress: c = 1 num = 385
Returning 385


DeCompression
===============
Read=135
uncompress: c = 135 num = 7
Read=4
uncompress: c = 4 num = 900
Returning 900


DeCompression
===============
Read=129
uncompress: c = 129 num = 1
Read=160
uncompress: c = 160 num = 160
Read=80
uncompress: c = 80 num = 20560
Returning 20560


DeCompression
===============
Read=131
uncompress: c = 131 num = 3
Read=0
Error: c is < 0 in uncompress1()

为什么我收到错误:当 DeCompress 384 号码时?

【问题讨论】:

标签: c compression


【解决方案1】:

你已经问过这个问题,我已经回答了here。零是编码方案中的预期字节,不得拒绝。只需删除 if (c &lt;= 0) 部分即可。

【讨论】:

  • 如果我不知道 0 那么我怎么知道文件结尾或行终止符到达。
  • 如果文件结尾对您不起作用,因为您在数字流之后还有其他数据并且数字的数量是可变的,那么您可以使用不允许的序列作为终止符。字节 128 是不允许的编码开始,因此可以终止数字列表。
  • 你是绝对正确的,但是如果你看到压缩是根据 7 位和 7 位不可能写 128 因此我写了 0 并且它充当终止符或错误符号
  • 这就是重点。 128 不能来自您对整数的编码。因此,将字节 128 与您的编码例程分开写入,以终止整数列表。在您的解码例程中,检测初始 128 并返回已到达流末尾的指示。
  • 我已经听取了您的建议,即 128 表示字符串的结尾,无论何时出现 0,它都会被更改为 128。但是数字 983055 出现另一个问题,压缩显示 188 128 15 返回 7680 和最后一个字节没有加进去。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-06
  • 2022-06-13
相关资源
最近更新 更多