在 32 位 Perl 中使用 64 位整数和十六进制

发布于 2024-12-03 09:32:03 字数 1157 浏览 2 评论 0原文

我想编写一个 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 技术交流群。

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

发布评论

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

评论(2

℉服软 2024-12-10 09:32:04

核心编译指示 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.

在风中等你 2024-12-10 09:32:04

Math::Int64 可以访问本机有符号和无符号 64 位数字。这肯定比可能的替代方案 Math::BigInt 更快。

它具有十六进制转换例程,并且重载比较和算术运算符,因此它可以完成您要求的一切。

use Math::Int64 qw( hex_to_uint64 uint64_to_hex );
my $n1 = hex_to_uint64("200000000");
my $n2 = hex_to_uint64("300000000");
printf("n1 is %s equal to n2\n",     $n1 == $n2 ? "" : "not");
printf("n1 is %s less than n2\n",    $n1 <  $n2 ? "" : "not");
printf("n1 is %s greater than n2\n", $n1 >  $n2 ? "" : "not");
printf("0x%016s", uint64_to_hex($n2 - $n1));

输出:

n1 is not equal to n2
n1 is less than n2
n1 is not greater than n2
0x0000000100000000

使用或缺少 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.

use Math::Int64 qw( hex_to_uint64 uint64_to_hex );
my $n1 = hex_to_uint64("200000000");
my $n2 = hex_to_uint64("300000000");
printf("n1 is %s equal to n2\n",     $n1 == $n2 ? "" : "not");
printf("n1 is %s less than n2\n",    $n1 <  $n2 ? "" : "not");
printf("n1 is %s greater than n2\n", $n1 >  $n2 ? "" : "not");
printf("0x%016s", uint64_to_hex($n2 - $n1));

Output:

n1 is not equal to n2
n1 is less than n2
n1 is not greater than n2
0x0000000100000000

The use of CPAN or lack thereof doesn't effect the portability of the script.

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