Pathologically Eclectic Rubbish Lister PerlMonks

### Re: Convert big number from decimal to hexadecimal

by ambrus (Abbot)
 on Jul 13, 2010 at 14:44 UTC ( #849292=note: print w/replies, xml ) Need Help??

Let me implement the other algorithm too. This one is longer, but you could cut some of it if you reorganized it so that there's only one function doing the work of all three.

```use 5.010;
{
no warnings "uninitialized";
my(\$a, \$b) = @_; my @b = @\$b; my \$c;
for (my \$k = 0; @b || \$c; \$k++) {
\$c = 10 <= (\$\$a[\$k] += \$c + shift @b); \$\$a[\$k] %= 10;
}
}
sub dsub {
my(\$a, \$b) = @_; my @b = @\$b; my \$c;
for (my \$k = 0; \$k < @\$a; \$k++) {
\$c = (\$\$a[\$k] -= \$c + shift @b) < 0; \$\$a[\$k] += 10 * \$
+c;
}
\$c;
}
sub dcmp {
my(\$a, \$b) = @_; my @a = @\$a; my @b = @\$b; my \$r;
while (@a || @b) {
if (my \$t = shift @a <=> shift @b) { \$r = \$t; }
}
\$r;
}
}
my \$n = [reverse split //, "4335043554366887798866555766"];
my \$p = [1];
my \$e = 0;
while (0 <= dcmp(\$n, \$p)) {
dadd \$p, \$p for 0..3; \$e++;
}
my \$r;
for my \$_e (0 .. \$e - 1) {
my \$i = 0;
while (0 <= dcmp (\$n, \$p)) {
dsub \$n, \$p; \$i++;
}
\$r .= sprintf "%x", \$i;
}
say \$r;
__END__

Replies are listed 'Best First'.
Re^2: Convert big number from decimal to hexadecimal
by ambrus (Abbot) on Jul 13, 2010 at 15:01 UTC

Okay, here's it a bit shortened:

```use 5.010;
sub dalu {
no warnings "uninitialized";
my(\$a, \$b, \$n, \$o) = @_; my @b = @\$b; my \$c = \$n && 1;
for (my \$k = 0; @b || \$k < @\$a || \$c && !\$n; \$k++) {
my \$y = shift @b; \$n and \$y = 9 - \$y;
\$c = 10 <= (my \$z = \$\$a[\$k] + \$c + \$y);
\$o or \$\$a[\$k] = \$z % 10;
}
\$c;
}
my \$n = [reverse split //, "4335043554366887798866555766"];
my \$p = [1];
my \$e = 0;
while (dalu \$n, \$p, 1, 1) {
dalu \$p, \$p for 0..3; \$e++;
}
my \$r;
for my \$_e (0 .. \$e - 1) {
dalu \$n, \$n for 0..3;
my \$i = 0;
while (dalu \$n, \$p, 1, 1) {
dalu \$n, \$p, 1; \$i++;
}
\$r .= sprintf "%x", \$i;
}
say \$r;
__END__

Update: made code a bit nicer.

Create A New User
Node Status?
node history
Node Type: note [id://849292]
help
Chatterbox?
 [Lady_Aleena]: Well, not much of my code. A few little things people here like. The big things though, the more important to me code, people here seem to loathe. [atcroft]: Lady_Aleena: My early code was pretty horrible... but somewhere early on I learned not to take criticism of my code personally... which was probably a good thing, considering the kind of code I wrote at the time. :\ [Lady_Aleena]: I am hungry, so I'm going to go make something to eat. I hope you figure it out atcroft. Have a nice night! [atcroft]: I have one script that I have started re-writing several times over the years, and have yet to get it working in a cleaner/better form. (It is some smelly code-I did not even know about hashes at the time I wrote it.) [atcroft]: Enjoy, Lady_Aleena!

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (7)
As of 2017-05-27 05:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
My favorite model of computation is ...

Results (192 votes). Check out past polls.