在c++中复制perl解包

Replicate unpack of perl in c++

本文关键字:perl 解包 复制 c++      更新时间:2023-10-16

我有一个像这样的ASCII二进制字符串

^ @ ^ @ ^ @ ^ ^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^ 一个 ^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^ 一个 ^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^ 一个 ^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^ 一个^ @ ^ @ ^ O  ^@^@^@^@^@^ BU - ^ @ ^ @ ^ @ ^ ^ @ ^ @ ^ O  ^@^@^@^@^@^ BU - ^ @ ^ @ ^ @ ^ ^ @ ^ @ ^ @^@^@^@^@^@^ 欧盟? ^ @ ^ @ ^ @ ^ ^ @ ^ @ ^ @^@^@^@^@^@^ 欧盟? ^ @ ^ @ ^ @ ^ ^ @ ^ @.a ^@^@^@^@^@^ Hað^ @ ^ @ ^ @ ^ ^ @ ^ @.a ^@^@^@^@^@^ Hað^ @ ^ @ ^ @ ^ ^ @ ^ @> & lt; 80年>^@^@^@^@^@^ KO<80> ^ @ ^ @ ^ @ ^ ^ @ ^ @> & lt; 80年> ^

Perl代码:

#!/usr/bin/env perl
use warnings;
use strict;
use Data::Dumper;
open(INDEX, $ARGV[0]);
binmode(INDEX);
my $buff;
my $ret = read(INDEX, $buff, 4);
my $fragment = unpack 'N', $buff;
$ret = read(INDEX, $buff, 4);
my $timestamp = unpack 'N', $buff;
$ret = read(INDEX, $buff, 8);
my $offset = unpack 'N', $buff;
print "timestamp = $timestamp fragment # $fragment offset = $offsetn";
输出:

timestamp = 0 fragment # 1 offset = 0
timestamp = 0 fragment # 1 offset = 0
timestamp = 0 fragment # 1 offset = 0
timestamp = 0 fragment # 1 offset = 0
timestamp = 4000 fragment # 1 offset = 187437
timestamp = 4000 fragment # 1 offset = 187437
timestamp = 8000 fragment # 1 offset = 384063
timestamp = 8000 fragment # 1 offset = 384063
timestamp = 12000 fragment # 1 offset = 582896
timestamp = 12000 fragment # 1 offset = 582896

我想在c++中复制上述unpack完成的工作怎么做呢?

我在尝试什么:

#include <iostream>
#include <stdlib.h>
using namespace std;
int main ()
{
    int k;
    char buf1[5];
    char buf2[5];
    char buf3[9];
    int i;
    char *str;
    //Assume str contains the entire binary data
   for(int i=0;str[i];)
   {
        while(k<32)
    {
            buf1[k]=str[i];
            ++k;++i;
    }
    k=0;
    while(k<32)
    {
            buf2[k]=str[i];
            ++k;
    }
    k=0;
    while(k<64)
    {
            buf3[k]=str[i];
            ++k;++i;
    }
    k=0;
    uint32_t a1,b1,a2,b2;
    uint64_t a3,b3;
    a1=atoi(buf1);
    b1=ntohl(a1);
    a2=atoi(buf2);
    b2=ntohl(a2);
    a3=atoi(buf1);
    b3=ntohl(a3);
    print "timestamp ="<< b2 << "fragment #"<<b1<<" offset ="<<b3<<"n";
    }
return 0;

}

给你:

#include <stdint.h>
#include <iostream>
#include <fstream>
using namespace std;
int main( int argc, char *argv[] )
{
    if (argc < 2)
    {
        cerr << "Input filename required" << endl;
        return 1;
    }
    ifstream f( argv[1], ios::binary );
    if (!f.good())
    {
        cerr << "Cannot open input file '" << argv[1] << "'" << endl;
        return 1;
    }
    while (!f.eof())
    {
        uint8_t buf[8];
        f.read( reinterpret_cast< char * >( buf ), 4 );
        uint32_t fragment = ( ( buf[0] * 256U + buf[1] ) * 256U + buf[2] ) * 256U + buf[3];
        f.read( reinterpret_cast< char * >( buf ), 4 );
        uint32_t timestamp = ( ( buf[0] * 256U + buf[1] ) * 256U + buf[2] ) * 256U + buf[3];
        f.read( reinterpret_cast< char * >( buf ), 8 );
        uint64_t offset = ( ( ( ( ( ( buf[0] * 256LL + buf[1] ) * 256LL + buf[2] ) * 256LL + buf[3] )
            * 256LL + buf[4] ) * 256LL + buf[5] ) * 256LL + buf[6] ) * 256LL + buf[7];
        if (f.good())
        {
            cout << "timestamp = " << timestamp;
            cout << " fragment = " << fragment;
            cout << " offset = " << offset << endl;
        }
    }
    return 0;
}

读取4个字节,然后使用

uint32_t n;
n = buf[0] << 24
  | buf[1] << 16
  | buf[2] <<  8
  | buf[3] <<  0;
例如,

uint32_t n;
unsigned char buf[4];
size_t bytes_read = fread (buf, 1, 4, stream);
if (bytes_read < 4) {
   if (ferror(stream)) {
      // Error
      // ...
   }
   else if (feof(stream)) {
      // Premature EOF
      // ...
   }
}
else {
   n = buf[0] << 24
     | buf[1] << 16
     | buf[2] <<  8
     | buf[3] <<  0;
}