# Any Base to Any base converter stops after 16 digits

Page 1 of 1

## 3 Replies - 2359 Views - Last Post: 02 August 2008 - 03:44 PM

### #1 evolivid

Reputation: 3
• Posts: 51
• Joined: 24-July 08

# Any Base to Any base converter stops after 16 digits

Posted 26 July 2008 - 12:00 AM

ok i got this perl code from born2code and it stops after 16 digits

here is the out put

Enter the sample base: 10
Enter base to convert to: 2
12345678901234567890
Digit 0 (0) is 0
Digit 1 (9) is 90
Digit 2 (8) is 800
Digit 3 (7) is 7000
Digit 4 (6) is 60000
Digit 5 (5) is 500000
Digit 6 (4) is 4000000
Digit 7 (3) is 30000000
Digit 8 (2) is 200000000
Digit 9 (1) is 1000000000
Digit 10 (0) is 0
Digit 11 (9) is 900000000000
Digit 12 (8) is 8000000000000
Digit 13 (7) is 70000000000000
Digit 14 (6) is 600000000000000
Digit 15 (5) is 5e+015
Digit 16 (4) is 4e+016
Digit 17 (3) is 3e+017
Digit 18 (2) is 2e+018
Digit 19 (1) is 1e+019
Base is 1.23456789012346e+019
1010101101010100101010011000110011101011000111110000100000000000

notice here at digit 15 it stops working properly
here is what it is suppose to look like in binary (thanks to Mathematica)

1010101101010100101010011000110011101011000111110000101011010010

so we can see that this might need bigfloat.pl or somthing else
i just cant figure out how to go about it

i tried to use fdiv(1,@num, 4000) in the code i lefta note where i tried to
use it but it didnt work is there somthing else that i could do
or can some one point me to another spot?

```#!perl.exe

require 'Bigfloat.pl';

my (\$sbase,\$tbase,\$sample)[email protected];
\$sbase= get_in( 'Enter the sample base:') unless(\$sbase );
\$tbase= get_in( 'Enter base to convert to:') unless(\$tbase );
if(\$sample) {

print convert_base(\$sbase,\$sample,\$tbase),"\n";
} else {

while(<STDIN>) {
chomp;
print convert_base(\$sbase,\$_,\$tbase),"\n";
}
}
sub convert_base {

my (\$sbase,\$sample,\$tbase)[email protected]_;
my @num=split(//,\$sample);
my (\$num,\$i,@res,@symb,%base);
foreach(0..9,A..Z,a..z) {

push @symb,\$_;
}
foreach([email protected]) {								 #no @symb-1

\$base{\$_}=\$symb[\$_];
}
@num=reverse(@num);	## i tried to use fdiv(1,@num, 4000) here
foreach([email protected]) {								 #no @num-1

print "Digit \$_ (" . \$num[\$_] . ") is ";					##
\$t=\$base{\$num[\$_]} * expn(\$sbase,\$_);
\$t=\$base{\$num[\$_]} if(\$_==0);
print \$t . "\n";										  ##
\$num+=\$t;
}
print "Base is \$num\n";			 ## print "Base is \$num\n"; //so dont use fdiv here
while(expn(\$tbase,\$i)<=\$num) {

\$i++;
}
\$i--;
foreach(0..\$i) {

\$_=\$i-\$_;
\$res[\$_]='0';
while(\$num>=expn(\$tbase,\$_)) {

\$num-=expn(\$tbase,\$_);
\$res[\$_]++;
}
}
foreach(@res) {

\$_=\$symb[\$_];
}
@res=reverse(@res);
return join('',@res);
}
sub expn {

my (\$num,\$expn)[email protected]_;
my \$n=1;
foreach(1..\$expn) {

\$n*=\$num;
}
return \$n;
}
sub get_in {

print shift() . ' ';
my \$data=<STDIN>;
chomp \$data;
return \$data;
}

```

Is This A Good Question/Topic? 0

## Replies To: Any Base to Any base converter stops after 16 digits

### #2 baavgai

• Dreaming Coder

Reputation: 7153
• Posts: 14,896
• Joined: 16-October 07

## Re: Any Base to Any base converter stops after 16 digits

Posted 26 July 2008 - 03:49 AM

Looks like the value is still maxing out at 32bits.

Maybe you want this?
```use Math::BigInt;

my \$n = Math::BigInt->new("12345678901234567890");
print \$n->as_bin(), "\n";

```

### #3 evolivid

Reputation: 3
• Posts: 51
• Joined: 24-July 08

## Re: Any Base to Any base converter stops after 16 digits

Posted 27 July 2008 - 08:20 PM

baavgai, on 26 Jul, 2008 - 03:49 AM, said:

Looks like the value is still maxing out at 32bits.

Maybe you want this?
```use Math::BigInt;

my \$n = Math::BigInt->new("12345678901234567890");
print \$n->as_bin(), "\n";

```

thank you that helped ! now i just need to spend some time improving the code
thankx`s again

" what I get done today I dont have to do Tomarrow ! "

### #4 evolivid

Reputation: 3
• Posts: 51
• Joined: 24-July 08

## Re: Any Base to Any base converter stops after 16 digits

Posted 02 August 2008 - 03:44 PM

ok i got this to work to an digit heres the code

``` #!perl.exe

require 'Bigfloat.pl';
use Math::BigInt;

#my \$n = Math::BigInt->new("12345678901234567890");
#
#print \$n->as_bin(), "\n";

##my \$num = Math::BigInt->new(\$sample);

my (\$sbase,\$tbase,\$sample)[email protected];
\$sbase= get_in( 'Enter the sample base:') unless(\$sbase );
\$tbase= get_in( 'Enter base to convert to:') unless(\$tbase );
if(\$sample) {

print convert_base(\$sbase,\$sample,\$tbase),"\n";
} else {

while(<STDIN>) {
chomp;
print convert_base(\$sbase,\$_,\$tbase),"\n";
}
}
sub convert_base {

my (\$sbase,\$sample,\$tbase)[email protected]_;
my @num=split(//,\$sample);
my (\$num,\$i,@res,@symb,%base);
foreach(0..9,A..Z,a..z) {

push @symb,\$_;
}
foreach([email protected]) {								 #no @symb-1

\$base{\$_}=\$symb[\$_];
}

@num=reverse(@num);
foreach([email protected]) {								 #no @num-1

##print "Digit \$_ (" . \$num[\$_] . ") is ";					##
\$t=\$base{\$num[\$_]} * expn(\$sbase,\$_);
\$t=\$base{\$num[\$_]} if(\$_==0);
##print \$t . "\n";										  ##
\$num+=\$t;
}
##print "Base is \$num\n";			 ## print "Base is \$num\n"; //so dont use fdiv here
while(expn(\$tbase,\$i)<=\$num) {

\$i++;
}
\$i--;
foreach(0..\$i) {

\$_=\$i-\$_;
\$res[\$_]='0';
while(\$num>=expn(\$tbase,\$_)) {

\$num-=expn(\$tbase,\$_);
\$res[\$_]++;
}
}
foreach(@res) {

\$_=\$symb[\$_];
}
@res=reverse(@res);
return join('',@res);
}
sub expn {

my (\$num,\$expn)[email protected]_;
my \$n=1;
foreach(1..\$expn) {

\$n*= Math::BigInt->new(\$num);				##\$n*= \$num;
}
return \$n;
}
sub get_in {
#my \$n = Math::BigInt->new(\$num);
#print \$n->as_bin(), "\n";

print shift() . ' ';
my \$data=<STDIN>;
chomp \$data;
return \$data;

}

```

" what I get done today I dont have to do Tomarrow ! "
[/quote]