在c++中复制perl解包
Replicate unpack of perl in c++
我有一个像这样的ASCII二进制字符串
Perl代码:^ @ ^ @ ^ @ ^ ^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^ 一个 ^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^ 一个 ^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^ 一个 ^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^ 一个^ @ ^ @ ^ O ^@^@^@^@^@^ BU - ^ @ ^ @ ^ @ ^ ^ @ ^ @ ^ O ^@^@^@^@^@^ BU - ^ @ ^ @ ^ @ ^ ^ @ ^ @ ^ @^@^@^@^@^@^ 欧盟? ^ @ ^ @ ^ @ ^ ^ @ ^ @ ^ @^@^@^@^@^@^ 欧盟? ^ @ ^ @ ^ @ ^ ^ @ ^ @.a ^@^@^@^@^@^ Hað^ @ ^ @ ^ @ ^ ^ @ ^ @.a ^@^@^@^@^@^ Hað^ @ ^ @ ^ @ ^ ^ @ ^ @> & lt; 80年>^@^@^@^@^@^ KO<80> ^ @ ^ @ ^ @ ^ ^ @ ^ @> & lt; 80年> ^
#!/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;
}
相关文章:
- struct.error:解压缩 C++ 结构时,解包需要 288 字节的缓冲区
- 没有取消引用/解包对象的标准方法?
- 模板函数签名在 C++/CLI 中解包
- msgpack:在不知道类型的情况下解包自定义类
- 正在将可变模板参数解包到初始值设定项列表中
- 如何在 C 中使用解包函数和 iota(复数"i")?
- 使用std::tie解包返回值会导致额外的成本吗?
- 多次解包参数包
- 如何正确解包来自提升异常的错误代码
- Autobahncpp 解包复杂的 msgpack 消息
- GCC 以错误的方式解包(我<...)折叠表达式
- 如果在可变参数模板解包中达到终止函数,则出现异常
- 如何在PHP中解包来自Qt struct/string的bool
- 将除最后一个作为变量类型的变量外的所有可变模板参数解包
- 将变量模板化类的模板参数解包为常量和常量数组
- C++等同于 PHP 函数包/解包
- 将 VT 解包与元序列交织在一起
- 可变参数模板函数解包顺序
- 我想了解C++中的参数解包
- 在c++中复制perl解包