如何在 C++ 中处理来自 Perl/PHP 的打包数据?

发布于 2024-08-19 23:40:06 字数 660 浏览 7 评论 0原文

我在用 C++ 实现 PHP 程序时遇到问题。它是关于 PHP/Perl 函数 unpack 的。我不知道如何在 C++ 中执行以下操作(读取文件没有问题......但我如何解压(“C *”)读取的内容)。

<?php
$file = fopen("bitmaskt.dat", "rb");
//create the data stream
$matrix_x           = unpack("C*", fread($file, 286));
$matrix_y           = unpack("C*", fread($file, 286));
$mask_data          = unpack("C*", fread($file, 286));
$reed_ecc_codewords = ord(fread($file, 1));
$reed_blockorder    = unpack("C*", fread($file, 128));
fclose($file);
?>

目前,我对自己解决这个问题感到非常绝望 - 我已经搜索了好几天,我发现的只是问题......有没有免费的 unpack() C++ 实现? :-(

I got a problem implementing a PHP programm in C++. It is about the PHP/Perl function unpack. I don't know how to do the follwing in C++ (no problem in reading a file... but how do i unpack("C*") the read contents).

<?php
$file = fopen("bitmaskt.dat", "rb");
//create the data stream
$matrix_x           = unpack("C*", fread($file, 286));
$matrix_y           = unpack("C*", fread($file, 286));
$mask_data          = unpack("C*", fread($file, 286));
$reed_ecc_codewords = ord(fread($file, 1));
$reed_blockorder    = unpack("C*", fread($file, 128));
fclose($file);
?>

Currently, I'm very hopeless solving this problem on my own - I'm searching for days, all I found are questions... Is there any free unpack() c++ implementation out there? :-(

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

私野 2024-08-26 23:40:06

Perl 的 pack 文档涵盖了 使用的模板>打包解包

假设您生成了 bitmaskt.dat

#! /usr/bin/perl

use warnings;
use strict;

open my $fh, ">", "bitmaskt.dat" or die "$0: open: $!";

my @data = (42) x 286;

print $fh pack("C*" => @data);
print $fh pack("C*" => @data);
print $fh pack("C*" => @data);
print $fh pack("C"  => 7);
print $fh pack("C*" => (1) x 128);

close $fh or warn "$0: close";

您可以使用以下命令读取它

#include <fstream>
#include <iostream>
#include <iterator>
#include <vector>

typedef unsigned char datum_t;
typedef std::vector<datum_t> buf_t;

std::istream &read_data(std::istream &in, buf_t &buf, size_t n)
{
  std::istreambuf_iterator<char> it(in.rdbuf()), eos;
  while (it != eos && n-- != 0)
    buf.push_back(static_cast<datum_t>(*it++));

  return in;
}

例如:

int main()
{
  std::ifstream bm("bitmaskt.dat", std::ifstream::binary | std::ifstream::in);

  struct {
    buf_t buf;
    size_t len;
    std::string name;
  } sections[] = {
    { buf_t(), 286, "matrix_x" },
    { buf_t(), 286, "matrix_y" },
    { buf_t(), 286, "mask_data" },
    { buf_t(),   1, "reed_ecc_codewords" },
    { buf_t(), 128, "reed_blockorder" },
  };
  const int n = sizeof(sections) / sizeof(sections[0]);

  for (int i = 0; n - i > 0; i++) {
    if (!read_data(bm, sections[i].buf, sections[i].len)) {
      std::cerr << "Read " << sections[i].name << " failed" << std::endl;
      return 1;
    }
  }

  const int codeword = 3;
  std::cout << (unsigned int) sections[codeword].buf[0] << '\n';

  return 0;
}

输出:

7

Perl's documentation for pack covers the templates used for pack and unpack.

Say you generated bitmaskt.dat with

#! /usr/bin/perl

use warnings;
use strict;

open my $fh, ">", "bitmaskt.dat" or die "$0: open: $!";

my @data = (42) x 286;

print $fh pack("C*" => @data);
print $fh pack("C*" => @data);
print $fh pack("C*" => @data);
print $fh pack("C"  => 7);
print $fh pack("C*" => (1) x 128);

close $fh or warn "$0: close";

You might read it with

#include <fstream>
#include <iostream>
#include <iterator>
#include <vector>

typedef unsigned char datum_t;
typedef std::vector<datum_t> buf_t;

std::istream &read_data(std::istream &in, buf_t &buf, size_t n)
{
  std::istreambuf_iterator<char> it(in.rdbuf()), eos;
  while (it != eos && n-- != 0)
    buf.push_back(static_cast<datum_t>(*it++));

  return in;
}

For example:

int main()
{
  std::ifstream bm("bitmaskt.dat", std::ifstream::binary | std::ifstream::in);

  struct {
    buf_t buf;
    size_t len;
    std::string name;
  } sections[] = {
    { buf_t(), 286, "matrix_x" },
    { buf_t(), 286, "matrix_y" },
    { buf_t(), 286, "mask_data" },
    { buf_t(),   1, "reed_ecc_codewords" },
    { buf_t(), 128, "reed_blockorder" },
  };
  const int n = sizeof(sections) / sizeof(sections[0]);

  for (int i = 0; n - i > 0; i++) {
    if (!read_data(bm, sections[i].buf, sections[i].len)) {
      std::cerr << "Read " << sections[i].name << " failed" << std::endl;
      return 1;
    }
  }

  const int codeword = 3;
  std::cout << (unsigned int) sections[codeword].buf[0] << '\n';

  return 0;
}

Output:

7
原谅过去的我 2024-08-26 23:40:06

我不知道 C++ 的 unpack 的任何一般实现,但这似乎不是您需要的东西。

如果matrix_x在某处定义为unsigned char matrix_x[286]并且您有一个打开的输入流inFile
那么你需要做的是inFile.get(matrix_x, 286)。这会从输入中读取 286 个字节,并将它们放入 matrix_x 指向的数组中。

I don't know about any general implementation of unpack for C++, but that doesn't seem to be the thing you need anyway.

if matrix_x is defined somewhere as unsigned char matrix_x[286] and you have an opened input stream inFile
then what you need to do is inFile.get(matrix_x, 286). This reads 286 bytes from the input and places them in the array pointed to by matrix_x.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文