用perl将增量计数器包装到十六进制中

发布于 2025-02-05 16:44:12 字数 1534 浏览 2 评论 0原文

我正在尝试创建一个12个字节标题,并且正在使用Pack。一切都很棒,直到我需要在包装功能中包含一个增量值。

编辑:

我有一个打开的FFMPEG管道,我正在捕获输出。捕获的数据将是一个有效载荷,将加密并附加到标头末端。我的循环不是循环,但我认为我会让读者更容易理解。

因此,让我在这里更新问题,希望它能清除任何混乱。如果我需要提供更多信息,请告诉我。

my $m = 00;
my $n = 00;

while( <$source_audio> ) { 
    if(($n % 16) == 0){
        $m++; #this should increment when n reaches 0xff
    }
    my $header = pack( 'C C n n C*', 0x80, 0x78, $m, $n, 0x12, 0x34, 0x98, 0x76, 0x01, 0x02, 0x03, 0x04 );
    print $file $header ;
    $n++;
}

我只需要能够打包$ m和$ n,所以我得到了预期的输出:

80 78 00 01 12 34 98 76 01 02 03 04
80 78 00 02 12 34 98 76 01 02 03 04
80 78 00 03 12 34 98 76 01 02 03 04
80 78 00 04 12 34 98 76 01 02 03 04
80 78 00 05 12 34 98 76 01 02 03 04
... iterating 0x0000 to 0xFFFF ...
80 78 FF FB 12 34 98 76 01 02 03 04
80 78 FF FC 12 34 98 76 01 02 03 04
80 78 FF FD 12 34 98 76 01 02 03 04
80 78 FF FE 12 34 98 76 01 02 03 04
80 78 FF FF 12 34 98 76 01 02 03 04

edit2:

这为我提供了所需的输出,但是我会启用严格和警告的错误:

my $m = 0x00;
my $n = 0x00;

my $message = "this";

while ( <$source_audio> ) {
    my $header = pack('C*', 0x80, 0x78, $m, $n, 0x12, 0x34, 0x98, 0x76, 0x01, 0x02, 0x03, 0x04);
    
    if(($n % 0xFF) == 0 and $n != 0x00){
        $m++;
    }
    
    $n++;
print $temp $header;
}
Character in 'C' format wrapped in pack

解决方案完全是Ikegami指出的:

pack('C C n C*', 0x80, 0x78, $n, 0x12, 0x34, 0x98, 0x76, 0x01, 0x02, 0x03, 0x04);

i am trying to create a 12 byte header and i am using pack. everything was going awesome until i needed to include an incrementing value in the pack function..

EDIT:

i have an open ffmpeg pipe that i am capturing the output from. the captured data is going to be a payload that will be encrypted and appended to the end of the header. my loop is not a for loop but i thought i would make it a little easier for the readers to understand.

so let me update the question here in hopes it clears any confusion. if i need to provide any more information let me know.

my $m = 00;
my $n = 00;

while( <$source_audio> ) { 
    if(($n % 16) == 0){
        $m++; #this should increment when n reaches 0xff
    }
    my $header = pack( 'C C n n C*', 0x80, 0x78, $m, $n, 0x12, 0x34, 0x98, 0x76, 0x01, 0x02, 0x03, 0x04 );
    print $file $header ;
    $n++;
}

i just need to be able to pack $m and $n so i get the expected output:

80 78 00 01 12 34 98 76 01 02 03 04
80 78 00 02 12 34 98 76 01 02 03 04
80 78 00 03 12 34 98 76 01 02 03 04
80 78 00 04 12 34 98 76 01 02 03 04
80 78 00 05 12 34 98 76 01 02 03 04
... iterating 0x0000 to 0xFFFF ...
80 78 FF FB 12 34 98 76 01 02 03 04
80 78 FF FC 12 34 98 76 01 02 03 04
80 78 FF FD 12 34 98 76 01 02 03 04
80 78 FF FE 12 34 98 76 01 02 03 04
80 78 FF FF 12 34 98 76 01 02 03 04

EDIT2:

this gives me the desired output, but i get errors with strict and warnings enabled:

my $m = 0x00;
my $n = 0x00;

my $message = "this";

while ( <$source_audio> ) {
    my $header = pack('C*', 0x80, 0x78, $m, $n, 0x12, 0x34, 0x98, 0x76, 0x01, 0x02, 0x03, 0x04);
    
    if(($n % 0xFF) == 0 and $n != 0x00){
        $m++;
    }
    
    $n++;
print $temp $header;
}
Character in 'C' format wrapped in pack

Solution was exactly as ikegami pointed out:

pack('C C n C*', 0x80, 0x78, $n, 0x12, 0x34, 0x98, 0x76, 0x01, 0x02, 0x03, 0x04);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清晰传感 2025-02-12 16:44:12

问题与pack或您使用它无关。问题是您正在错误地计算$ M$ n。警告是$ m超过0xff的结果,显然不应该发生。

以下正确计算$ m$ n(我将其重命名为$ hi$ lo,原因是很快就会变得很明显):

my $hi = 0x00;
my $lo = 0x00;
while ( <$source_audio> ) {
    my $header = pack( 'C*',
       0x80, 0x78, $hi, $lo, 0x12, 0x34, 0x98, 0x76, 0x01, 0x02, 0x03, 0x04 );
    
    ...

    if ( $lo == 0xFF ) {
        $lo = 0;
        ++$hi;
    } else {
        ++$lo;
    }
}

这也可以按照以下方式写:

    $lo = ( $lo + 1 ) & 0xFF;
    ++$hi if !$lo;

但是我们实际上只是在增加一个数字,该数字存储在两个八位字中。

my $n = 0;
while ( <$source_audio> ) {
    my $hi = $n >> 8;
    my $lo = $n & 0xFF;
    my $header = pack( 'C*',
       0x80, 0x78, $hi, $lo, 0x12, 0x34, 0x98, 0x76, 0x01, 0x02, 0x03, 0x04 );
    
    ...

    ++$n;
}

但是,让我们把包装留给pack!我们正在尝试使用Big-Endian字节订单将该值存储为16位未签名的INT。咨询用于包装和解开数字的格式

my $n = 0;
while ( <$source_audio> ) {
   my $header = pack( 'C C n C*',
      0x80, 0x78, $n, 0x12, 0x34, 0x98, 0x76, 0x01, 0x02, 0x03, 0x04 );
    
    ...

    ++$n;
}

The problem has nothing to do with pack or your use of it. The problem is that you are miscalculating $m and $n. The warning is the result of $m exceeding 0xFF, which clearly should not happen.

The following properly calculates $m and $n (which I renamed to $hi and $lo for reasons that will soon become obvious):

my $hi = 0x00;
my $lo = 0x00;
while ( <$source_audio> ) {
    my $header = pack( 'C*',
       0x80, 0x78, $hi, $lo, 0x12, 0x34, 0x98, 0x76, 0x01, 0x02, 0x03, 0x04 );
    
    ...

    if ( $lo == 0xFF ) {
        $lo = 0;
        ++$hi;
    } else {
        ++$lo;
    }
}

This could also be written as follows:

    $lo = ( $lo + 1 ) & 0xFF;
    ++$hi if !$lo;

But we're really just incrementing one number which is stored across two octets.

my $n = 0;
while ( <$source_audio> ) {
    my $hi = $n >> 8;
    my $lo = $n & 0xFF;
    my $header = pack( 'C*',
       0x80, 0x78, $hi, $lo, 0x12, 0x34, 0x98, 0x76, 0x01, 0x02, 0x03, 0x04 );
    
    ...

    ++$n;
}

But let's leave the packing to pack! We're trying to store the value as a 16 bit unsigned int using big-endian byte order. Consulting Formats for Packing and Unpacking Numbers or the documentation for pack shows we can use S> or n.

my $n = 0;
while ( <$source_audio> ) {
   my $header = pack( 'C C n C*',
      0x80, 0x78, $n, 0x12, 0x34, 0x98, 0x76, 0x01, 0x02, 0x03, 0x04 );
    
    ...

    ++$n;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文