Re: OT: Need help with numeric bases
From: | H. S. Teoh <hsteoh@...> |
Date: | Tuesday, February 25, 2003, 1:12 |
On Mon, Feb 24, 2003 at 07:58:35PM -0500, H. S. Teoh wrote:
> On Mon, Feb 24, 2003 at 05:35:38PM -0600, Peter Clark wrote:
[snip]
> > Hmm...this is going to be an interesting mini-challenge to whip up a script
> > that will convert between ash[*] and decimal. I would need to keep track of
> > whether a zero has already occured or not, as well as some system by which a
> > preceeding digit can be decremented to either 8 or 7.
> [snip]
>
> #!/usr/bin/perl
> sub ash{my$n=$_[0]-1;($n>=0)?ash(int($n/8)).($n%8+1):""}print ash(@ARGV),"\n"
[snip]
I should note that conversion from ash back to decimal uses the exact same
algorithm that converts octal to decimal:
#!/usr/bin/perl
map{$acc=$acc*8+$_;}split//,$ARGV[0];print"$acc\n";
The corresponding human-readable version would be:
#!/usr/bin/perl
foreach $digit ( split //, $ARGV[0] ) {
$acc = $acc*8 + $digit;
}
print "$acc\n";
:-)
T
--
Once bitten, twice cry...
Replies