上一篇
在计算机科学中,游程编码(Run-Length Encoding,简称RLE)是一种简单而高效的数据压缩技术。它特别适用于包含大量连续重复字符的数据,比如黑白图像、日志文件或某些类型的文本。本教程将带你从零开始,用C语言实现一个完整的游程编码与解码程序,即使是编程新手也能轻松掌握!
游程编码的基本思想是:将连续重复的字符(称为“游程”)替换为“重复次数 + 字符”这一对信息。例如:
AAAAABBBCCD5A3B2C1D可以看到,原本11个字符被压缩成了8个字符,实现了初步的数据压缩效果。
下面我们用C语言编写两个函数:rle_encode() 用于压缩,rle_decode() 用于解压。
#include <stdio.h>#include <string.h>#include <stdlib.h>char* rle_encode(const char* input) { if (input == NULL) return NULL; int len = strlen(input); if (len == 0) return strdup(""); // 分配足够大的缓冲区(最坏情况:每个字符都不同) char* output = (char*)malloc(len * 4); // 每个字符最多占4字节(如999A) if (!output) return NULL; int out_index = 0; int i = 0; while (i < len) { char current = input[i]; int count = 1; // 统计连续相同字符的数量 while (i + count < len && input[i + count] == current) { count++; } // 将计数和字符写入输出缓冲区 out_index += sprintf(output + out_index, "%d%c", count, current); i += count; } return output;} char* rle_decode(const char* encoded) { if (encoded == NULL) return NULL; int len = strlen(encoded); if (len == 0) return strdup(""); // 估算最大输出长度(保守估计) char* output = (char*)malloc(len * 10); if (!output) return NULL; int out_index = 0; int i = 0; while (i < len) { // 读取数字部分 int count = 0; while (i < len && encoded[i] >= '0' && encoded[i] <= '9') { count = count * 10 + (encoded[i] - '0'); i++; } // 读取字符 if (i < len) { char ch = encoded[i]; for (int j = 0; j < count; j++) { output[out_index++] = ch; } i++; } } output[out_index] = '\0'; return realloc(output, out_index + 1); // 调整内存大小} int main() { const char* original = "AAAAABBBCCD"; printf("原始字符串: %s\n", original); char* compressed = rle_encode(original); printf("压缩后: %s\n", compressed); char* decompressed = rle_decode(compressed); printf("解压后: %s\n", decompressed); // 验证是否还原成功 if (strcmp(original, decompressed) == 0) { printf("✅ 压缩与解压成功!\n"); } else { printf("❌ 出现错误!\n"); } // 释放内存 free(compressed); free(decompressed); return 0;} free(),避免内存泄漏。通过本教程,你已经掌握了如何用C语言实现基本的游程编码算法。这项技术虽然简单,但在图像处理(如BMP、PCX格式)、传真传输等领域仍有广泛应用。理解RLE是学习更高级数据压缩算法(如LZW、Huffman编码)的良好起点。
现在,你可以尝试修改代码,支持更多字符类型,或将其封装成库函数供其他项目调用。动手实践是掌握Run-Length Encoding教程的最佳方式!
本文由主机测评网于2025-12-10发表在主机测评网_免费VPS_免费云服务器_免费独立服务器,如有疑问,请联系我们。
本文链接:https://www.vpshk.cn/2025125763.html