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offset = unpack 'N', $buff;

print "timestamp = $timestamp fragment # $fragment offset = $offset\n";

输出:

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 ++中解压缩来完成上述工作 怎么做?

我在尝试什么:

#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;

}

2 个答案:

答案 0 :(得分:2)

你走了:

#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;
}

答案 1 :(得分:-1)

读取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;
}