用perl将增量计数器包装到十六进制中
我正在尝试创建一个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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题与
pack
或您使用它无关。问题是您正在错误地计算$ M
和$ n
。警告是$ m
超过0xff
的结果,显然不应该发生。以下正确计算
$ m
和$ n
(我将其重命名为$ hi
和$ lo
,原因是很快就会变得很明显):这也可以按照以下方式写:
但是我们实际上只是在增加一个数字,该数字存储在两个八位字中。
但是,让我们把包装留给
pack
!我们正在尝试使用Big-Endian字节订单将该值存储为16位未签名的INT。咨询用于包装和解开数字的格式或 。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
exceeding0xFF
, 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):This could also be written as follows:
But we're really just incrementing one number which is stored across two octets.
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 forpack
shows we can useS>
orn
.