I have a large hex number - $num = 0x80000000
, as a string.
I want to increment it, but doing hex($num)
does not work, due to integer overflow (comes out negative). using bigint
is also not an option as hex with bigint is only implemented in perl 5.10 and beyond, I have 5.8. how can I ++ this string?
How can I increment a hex string in Perl 5.8?
1.6k views Asked by WeaselFox At
2
There are 2 answers
1

Don't confuse the literal representation with the number with the actual value. When you make the assignment, no matter how you represented it, Perl ends up storing a number and no longer cares about the original representation. Use the normal numeric operations on it. When you want to look at it again, you can choose any of the representations you like:
$num = 0x8000000;
$num++;
printf "%0x" $num;
You only need hex()
if you're getting your numeric representation as a string, like you would from a command line argument. You only need hex
to turn the string into a number. After that, it's the same.
$num = hex( '8000000' ); # or hex( '0x8000000' )
# $num = hex( $ARGV[0] );
$num++;
printf "%0x" $num;
For the other part of your question, bignum
works just fine on Perl v5.8:
$ perl5.8.9 -le 'my $i = 0xFFFFFFFF_FFFFFFFF; $i++; print $i'
1.84467440737096e+19
$ perl5.8.9 -Mbignum -le 'my $i = 0xFFFFFFFF_FFFFFFFF; $i++; print $i'
18446744073709551616
I have no issues with this using Perl 5.8.9 via Perlbrew:
This prints out
2147483648
which is the decimal value of0x8000000
.What platform are you on?
What you probably want to do is to print out this hex number in hex and not decimal. You can still use
++
to increment it, but to print it out in hex, you need printf:This prints out:
If indeed
0x80000000
is a string, using hex on my system using Perl 5.8.9 has no problem converting it into a number.As I said. This is Perl 5.8.9 (I can't get 5.8.8 on Perlbrew), and this is on a Mac. Maybe your platform is a wee bit different. Is it an old Solaris system with a 32bit version of SunOS or Solaris?
I've checked the 5.8.8 documentation and notice that the standard distribution does have Bigint support built in. It also comes with the module Math::Bigint too.
Are you sure you don't have Bigint support?