【发布时间】:2020-01-27 14:07:32
【问题描述】:
鉴于http://michael.dipperstein.com/lzw/#example1 页面上 example1 的输入,我无法得到正确的结果:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "lzw.h"
void print_hex(unsigned char str[], int len)
{
int idx;
for (idx = 0; idx < len; idx++)
printf("%02x", str[idx]);
}
int main()
{
FILE *fpIn; /* pointer to open input file */
FILE *fpOut; /* pointer to open output file */
FILE *fptr;
char test_str_lzw[] = { "this_is_his_thing" };
fptr = fopen("lzw_in_test.txt", "wb");
fwrite(test_str_lzw, sizeof(char), strlen(test_str_lzw), fptr);
fclose(fptr);
fpIn = fopen("lzw_in_test.txt", "rb");
fpOut = fopen("lzw_out.txt", "wb");
LZWEncodeFile(fpIn, fpOut);
fclose(fpIn);
fclose(fpOut);
// Getting the results from file
if ((fptr = fopen("lzw_out.txt", "rb")) == NULL) {
printf("Error! opening file");
// Program exits if file pointer returns NULL.
exit(1);
}
unsigned char lzw_out[256];
memset(lzw_out, 0, 256);
size_t num;
num = fread(lzw_out, sizeof(unsigned char), 256, fptr);
fclose(fptr);
unsigned int lzw_size = num;
printf("LZW out size: %d\n", lzw_size);
printf("LZW out data: \n");
print_hex(lzw_out, lzw_size);
printf("\n");
return(0);
}
十六进制的预期结果:
0x74 0x68 0x69 0x73 0x5F 0x102 0x5F 0x101 0x103 0x100 0x69 0x6E 0x67
我得到十六进制的结果:
0x74 0x34 0x1A 0x4E 0x65 0xF0 0x15 0x7C 0x03 0x03 0x80 0x5A 0x4D 0xC6 0x70 0x20
谁能帮我获取示例中的输出文件?
问候。
【问题讨论】:
-
请在您的问题中包含您想要的结果和您得到的结果(或完整的错误消息)。此外,
LZWEncodeFile未定义。谢谢! -
@mzuther 我已经编辑了我的问题,我已经添加了 lzw.h 包含所以现在 LZWEncodeFile 已定义。我还添加了预期的和我想要的结果。谢谢。
-
@Embedded Aside:
sizeof(test_str_lzw)的大小为 18。strlen(test_str_lzw)的长度为 17。您要压缩多少 字符串? -
发布如何从代码中查看结果。 “我得到的十六进制结果”是正确的位流,而不是正确的包装。 (注意 9 位在预期与 8 位在看到)发布minimal reproducible example
-
@chux-ReinstateMonica 我已经编辑了问题,所以现在您可以看到最小的可重现示例以及我如何显示数据。
标签: c compression lzw