gpt4 book ai didi

c++ - 在 C++ 中复制 perl 的解包

转载 作者:行者123 更新时间:2023-11-28 06:46:51 24 4
gpt4 key购买 nike

<分区>

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

^@^@^@^A^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^A^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^A^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^A^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^A^@^@^O ^@^@^@^@^@^BÜ-^@^@^@^A^@^@^O ^@^@^@^@^@^BÜ-^@^@^@^A^@^@^@^@^@^@^@^@^EÜ?^@^@^@^A^@^@^@^@^@^@^@^@^EÜ?^@^@^@^A^@^@.à^@^@^@^@^@^Häð^@^@^@^A^@^@.à^@^@^@^@^@^Häð^@^@^@^A^@^@><80>^@^@^@^@^@^KÖ<80>^@^@^@^A^@^@><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 = $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;

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com