gpt4 book ai didi

PHP (ZLIB) 解压缩 C (ZLIB) 压缩数组会返回乱码

转载 作者:行者123 更新时间:2023-11-30 15:59:09 27 4
gpt4 key购买 nike

我有一组存储在数据库中的 ZLIB 压缩/base64 编码字符串(在 C 程序中完成)。我编写了一个小型 PHP 页面,应该检索这些值并绘制它们(字符串最初是 float 列表)。

压缩/编码的 C 程序 block :

error=compress2(comp_buffer, &comp_length,(const Bytef*)data.mz ,(uLongf)length,Z_DEFAULT_COMPRESSION); /* compression */
if (error != Z_OK) {fprintf(stderr,"zlib error..exiting"); exit(EXIT_FAILURE);}
mz_binary=g_base64_encode (comp_buffer,comp_length); /* encoding */

原始输入格式(示例):

292.1149    8379.5928
366.1519 101313.3906
367.3778 20361.8105
369.1290 17033.3223
375.4355 1159.1841
467.3191 8445.3926

每一列都被压缩/编码为单个字符串。为了重建原始数据,我使用以下代码:

//$row[4] is retrieved from the DB and contains the compressed/encoded string
$mz = base64_decode($row[4]);
$unc_mz = gzuncompress($mz);
echo $unc_mz;

但这给了我以下输出:

f6jEÍ„]EšiSE@IEfŽ

有人可以给我一些关于我可能缺少什么的提示吗?

------添加信息-----

我觉得问题来自于这样一个事实:当前 php 将 $unc_mz 视为单个字符串,而实际上我必须重新构造一个包含 X 行的数组(此输出来自 9 行文件)但是.. .不知道如何完成该作业。

执行此操作的 C 程序大致如下:

 uncompress( pUncompr , &uncomprLen , (const Bytef*)pDecoded , decodedSize );
pToBeCorrected = (char *)pUncompr;
for (n = 0; n < (2 * peaksCount); n++) {
pPeaks[n] = (RAMPREAL) ((float *) pToBeCorrected)[n];
}

其中peaksCount是输入文件中“行”的数量。

<小时/>

编辑(15-2-2012):我的代码的问题是我没有重建数组,固定代码如下(如果有人需要类似的代码片段,可能会很方便) :

  while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
$m< = base64_decode($row[4]);
$mz_int = gzuncompress($int);
$max = strlen($unc_mz);
$counter = 0;
for ($i = 0; $i < $max; $i = $i + 4) {
$temp= substr($unc_mz,$i,4);
$temp = unpack("f",$temp);
$mz_array[$counter] = $temp[1];
$counter++;
}

未压缩的字符串必须被切成与浮点长度相对应的 block ,然后 unpack() 从二进制“ block ”重建 float 据。这是我可以对上述代码片段给出的最简单的描述。

最佳答案

compress2() 生成 zlib 格式 (RFC 1950)。我不得不猜测名为 gzuncompress() 的东西需要 gzip 格式(RFC 1952)。因此,如果找不到 gzip header ,gzuncompress() 就会立即失败。

您需要在 zlib 中使用 deflateInit2() 来请求 deflate() 生成 gzip 格式的输出,或者在 PHP 中查找或提供需要 zlib 格式的不同函数。

关于PHP (ZLIB) 解压缩 C (ZLIB) 压缩数组会返回乱码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9211955/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com