高效的 p​​re-perl-5.10 等效包(“Q") [英] Efficient pre-perl-5.10 equivalent of pack("Q>")

查看:45
本文介绍了高效的 p​​re-perl-5.10 等效包(“Q")的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:Salva 正确地指出我对Q"包模板的引入是错误的.是 ">" 修饰符不会回到 5.8.

Update: Salva correctly points out that I was wrong about the introduction of the "Q" pack template. It's the ">" modifier that doesn't go back to 5.8.

Perl 5.10 引入了 pack() 修饰符>",对于我使用Q"的用例,它以 big endian 格式打包一个无符号四元(64 位)值.

Perl 5.10 introduced the pack() modifier ">", which, for my use case with "Q" packs an unsigned quad (64bit) value in big endian.

现在,我正在寻找一个有效的等价物

Now, I'm looking for an efficient equivalent for

pack("Q>2", @ints)

其中@ints 包含两个 64 位无符号整数.Q>2"表示以大端字节顺序打包两个无符号四边形".显然,我想要这个是因为我(至少暂时)依赖于 5.10 之前的 Perl.

where @ints contains two 64bit unsigned ints. "Q>2" means "pack two unsigned quads in big-endian byte order". Obviously, I want this because I am (at least temporarily) tied to a pre-5.10 Perl.

更新 2:实际上,经过进一步思考,应该做一些简单的事情:

Update2: Actually, on further reflection, something as simple as the following should do:

pack("N4", $ints[0] >> 32, $ints[0], $ints[1] >> 32, $ints[1])

似乎适用于我的 64 位 x86-64 Linux.这可能与 pack("Q>2", @ints) 不完全相同的任何原因?任何特定于平台的问题?

Appears to work on my 64bit x86-64 Linux. Any reason why this might not be exactly the same as pack("Q>2", @ints)? Any platform-specific matters?

什么是反向(即相当于 unpack("Q>2", @ints))?

What's the reverse (ie. equivalent to unpack("Q>2", @ints))?

推荐答案

Q 模式是在 perl 5.6 中引入的.您真正的问题可能是您试图在没有 64 位支持的情况下编译的 perl 中使用它.

The Q pattern was introduced in perl 5.6. Your real problem may be that you are trying to use it in a perl compiled without 64bit support.

无论如何,您可以使用 Math::Int64.

Anyway, you can use Math::Int64.

更新,示例:

use Math::Int64 qw(int64_to_native);
my $packed = join '', map int64_to_native($_), @ints;

另一种选择,如果您使用的是支持 Q 但不支持 Q> 的 64 位 perl,则自己重新排列字节:

Another option, if you are on a 64bit perl supporting Q but not Q>, is to reorder the bytes yourself:

pack 'C*', reverse unpack 'C*', pack 'Q', $int;

这篇关于高效的 p​​re-perl-5.10 等效包(“Q")的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆