There's more than one way to do things PerlMonks

### Re: long integer to hexadecimal conversion

by Joost (Canon)
 on Oct 01, 2009 at 10:49 UTC ( #798603=note: print w/ replies, xml ) Need Help??

bigint / Math::BigInt are core modules, and do what you want.

Comment on Re: long integer to hexadecimal conversion
Replies are listed 'Best First'.
Re^2: long integer to hexadecimal conversion
by kikuchiyo (Monk) on Oct 01, 2009 at 10:58 UTC
I wanted to say the same thing, but I'm late, I guess:

```use bigint;

my \$n = 123456789123456;
my (\$h, \$d);
while (\$n > 0) {
\$d = \$n % 16;
\$n /= 16;
\$h .= sprintf "%x",\$d;
}
print scalar reverse \$h;
Much simpler:
```use bigint;
my \$n = 123456789123456;
print \$n->as_hex;
or just
```use bigint;
print 123456789123456->as_hex
True.

You'd only need my algorithm if you wanted to express the integer in a base other than 16 (or 8).

```use bigint;

my @digits=map chr,(0x30..0x39,0x41..0x5d);
my \$base = 22;
my \$n = 123456789123456;
my \$h;
while(\$n > 0){
\$h .= \$digits[ \$n % \$base ];
\$n /= \$base;
}
print scalar reverse \$h';
Result is 4E5G93GHBE8.
Super Duper Cool Thanks Joost
couldn't have been easier than this. Thanks kikuchiyo, you saved my day

Create A New User
Node Status?
node history
Node Type: note [id://798603]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (10)
As of 2016-05-05 13:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
What font do you use for programming?

Results (93 votes). Check out past polls.