在C++中复制Perl的解包
我有一个ASCII码的二进制字符串,如下所示 ^@^@^@^本\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\本\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\本港的整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体Ö^@^@>^ Perl代码:在C++中复制Perl的解包,c++,perl,pack,unpack,C++,Perl,Pack,Unpack,我有一个ASCII码的二进制字符串,如下所示 ^@^@^@^本\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\本\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\本港的整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整体整
#!/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 = $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;
}读取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;
}
给你:
#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;
}
我希望C++代码做perl脚本的操作!1算出每个字节值代表什么,2读入,3使用1中的细节,将它们重新组合成所需的时间值。您可以从一个std::ifstream开始读取,我猜这些值将采用某种相当标准的格式:您可能可以将它们直接读取到uintint32\t中,并且可能需要对它们进行运行。当你真的尝试了一些东西,人们会帮助你的时候,发布你的代码。对不起,我添加了我正在做的事情。你能纠正我吗?如果你真的描述你想要什么,可能会有很大帮助。如果说我有一堆不可读的输入和一堆同样不可读的Perl,那么读者就承担了理解事情的全部责任。你的C++是更好的,但你仍然不能告诉我们你打算做什么,以及你得到的结果与你想要的不同。Perl代码是很可读的。非常感谢Khouri Giordano!它就像一个符咒。一个小疑问。如果输入存储在std::string中而不是文件中会怎么样?在这种情况下,使用什么来代替f.read重新解释\u cast