in reply to Re^5: Big Int to hex with 5.6
in thread Big Int to hex with 5.6

Arg ! But i need a way to do it ! Here's a nice function i found that converts from hex to dec using only one BigInt.
#!perl use strict; use Math::BigInt; sub HeX { my $n = shift; $n = '0' x (4 - (length($n) % 4)) . $n; my $m = Math::BigInt->new(0); while (my $o = substr($n, 0, 4, '')){ $m = $m * 65536 + hex $o; } $m; }
I'm trying to find a way to do something similar but from dec to hex . Maybe you have an idea ?

Replies are listed 'Best First'.
Re^7: Big Int to hex with 5.6
by ysth (Canon) on Dec 13, 2004 at 00:42 UTC
    This works for me on cygwin perl 5.6.2 (Math::BigInt version 0.01):
    #!/usr/local/bin/perl5.6.2 use Math::BigInt; BEGIN { if (!exists &Math::BigInt::as_hex) { *Math::BigInt::as_hex = sub { my $i = shift; my $hex = ''; do { my $mod = $i % 16; $hex .= sprintf("%x", $mod); } while $i = $i / 16; scalar reverse $hex; } } } $z = new Math::BigInt("1234567890"x 3); print $z->as_hex();
    Slight variations failed completely, though :)
      wow !

      it works :)

      thanks a lot, this really saves my day !

Re^7: Big Int to hex with 5.6
by fglock (Vicar) on Dec 13, 2004 at 04:06 UTC

    This is the as_hex() function from Activestate 5.8.0 (which is actually different from the one currently in CPAN):