Important alert: (current site time 7/15/2013 9:12:04 PM EDT)
 

VB icon

Convert base X to base N Numbers

Email
Submitted on: 6/30/2001 4:32:23 AM
By: RyMon  
Level: Intermediate
User Rating: By 2 Users
Compatibility: 5.0 (all versions), Active Perl specific, 4.0 (all versions), 3.0 (all versions), Pre 3.0
Views: 12930
(About the author)
 
     Converts numbers in any base to any other base
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
=**************************************
= Name: Convert base X to base N Numbers
= Description:Converts numbers in any base to any other base
= By: RyMon
=
= Inputs:Base of sample number, the sample number, and the new base to convert to
=
= Returns:A fully converted, base n number
=
=This code is copyrighted and has= limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=211&lngWId=6=for details.=**************************************

#!perl.exe
my ($sbase,$tbase,$sample)=@ARGV;
$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)=@_;
	my @num=split(//,$sample);
	my ($num,$i,@res,@symb,%base);
	foreach(0..9,A..Z,a..z) {
		push @symb,$_;
	}
	foreach(0..@symb-1) {
		$base{$_}=$symb[$_];
	}
	@num=reverse(@num);
	foreach(0..@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";
	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)=@_;
	my $n=1;
	foreach(1..$expn) {
		$n*=$num;
	}
	return $n;
}
sub get_in {
	print shift() . ' ';
	my $data=<STDIN>;
	chomp $data;
	return $data;
}


Other 1 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

8/18/2001 12:02:40 PMTim

The input instructions are the wrong way round ... it should be , , .
No good for working out numbers less than 0, as I was trying to do! :-)
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.