in reply to Re^6: Big Int to hex with 5.6
in thread Big Int to hex with 5.6
This is the as_hex() function from Activestate 5.8.0 (which is actually different from the one currently in CPAN):
sub as_hex { # return as hex string, with prefixed 0x my $x = shift; $x = $class->new($x) if !ref($x); return $x->bstr() if $x->{sign} !~ /^[+-]$/; # inf, nan etc return '0x0' if $x->is_zero(); my $es = ''; my $s = ''; $s = $x->{sign} if $x->{sign} eq '-'; if ($CALC->can('_as_hex')) { $es = ${$CALC->_as_hex($x->{value})}; } else { my $x1 = $x->copy()->babs(); my ($xr,$x10000,$h); if ($] >= 5.006) { $x10000 = Math::BigInt->new (0x10000); $h = 'h4'; } else { $x10000 = Math::BigInt->new (0x1000); $h = 'h3'; } while (!$x1->is_zero()) { ($x1, $xr) = bdiv($x1,$x10000); $es .= unpack($h,pack('v',$xr->numify())); } $es = reverse $es; $es =~ s/^[0]+//; # strip leading zeros $s .= '0x'; } $s . $es; }
|
|---|