在 32 位 Perl 中使用 64 位整数和十六进制
我想编写一个 perl 脚本来解析其中包含大量 64 位整数的文本文件。所有整数均以十六进制书写。
我需要
- 从输入中读取十六
- 进制比较 64 位整数 (
<
,=
,>
) - 减去 64 位整数
- 输出 64-位十六进制
我需要使用 32 位 perl,并且不能使用任何 CPAN/外部模块(脚本必须是可移植的)。
PS我的perl是5.8(这是将用于脚本的最小版本)
PPS bignum/bigint错误:
$ perl -e 'use bignum; $_=hex("0x0000123412345678")'
Integer overflow in hexadecimal number at -e line 1.
$ perl -e 'use bigint; $_=hex("0x0000123412345678")'
Integer overflow in hexadecimal number at -e line 1.
PPPS:这里没有from_hex
。
$ perl -e 'use Math::BigInt; $_=Math::BigInt->from_hex("0x0000123412345678");'
Can't locate object method "from_hex" via package "Math::BigInt" at -e line 1.
并且没有 qw/hex/
:
$ perl -e 'use bigint qw/hex/; $_=hex("0x0000123412345678")'
unknown option hex at /usr/lib/perl5/5.8/bigint.pm line...
PPPPS:但 new() 有效:
$ perl -e 'use Math::BigInt; $_=Math::BigInt->new("0x0000123412345678"); print $_->as_hex(),"\n";'
0x123412345678
I want to write a perl script to parse text files with a lot of 64-bit integers in it. All integers are written in hex.
I need to
- Read hexes from input
- Compare 64-bit ints (
<
,=
,>
) - Subtract 64-bit ints
- Output 64-bit hexes
I need to use 32-bit perl and I can't use any CPAN/external module (the script must be portable).
PS my perl is 5.8 (and this is minimal version which will be used for the script)
PPS bignum/ bigint errors:
$ perl -e 'use bignum; $_=hex("0x0000123412345678")'
Integer overflow in hexadecimal number at -e line 1.
$ perl -e 'use bigint; $_=hex("0x0000123412345678")'
Integer overflow in hexadecimal number at -e line 1.
PPPS: no from_hex
here.
$ perl -e 'use Math::BigInt; $_=Math::BigInt->from_hex("0x0000123412345678");'
Can't locate object method "from_hex" via package "Math::BigInt" at -e line 1.
And no qw/hex/
:
$ perl -e 'use bigint qw/hex/; $_=hex("0x0000123412345678")'
unknown option hex at /usr/lib/perl5/5.8/bigint.pm line...
PPPPS: but new() works:
$ perl -e 'use Math::BigInt; $_=Math::BigInt->new("0x0000123412345678"); print $_->as_hex(),"\n";'
0x123412345678
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
核心编译指示 bigint 将让您透明地使用大于系统支持的整数。 Math::BigInt 核心库中有相关函数可用于十六进制之间的转换表示。
The core pragma bigint will let you transparently work with integers larger than your system can support. There are associated functions in the Math::BigInt core library to convert from and to a hex representation.
Math::Int64 可以访问本机有符号和无符号 64 位数字。这肯定比可能的替代方案 Math::BigInt 更快。
它具有十六进制转换例程,并且重载比较和算术运算符,因此它可以完成您要求的一切。
输出:
使用或缺少 CPAN 不会影响脚本的可移植性。
Math::Int64 gives access to native signed and unsigned 64-bit numbers. This is surely faster than possible alternative Math::BigInt.
It has hex conversion routines, and it overloads comparison and arithmetic operators, so it can do everything you asked for.
Output:
The use of CPAN or lack thereof doesn't effect the portability of the script.