#!/usr/bin/perl # base-convert - command-line program for converting from one base to another # $Id: base-convert,v 1.5 2003/08/26 10:28:17 cvs-michael Exp $ # Copyright (c) 2002 Michael Fowler # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA use Getopt::Long; use warnings; use strict; my($debug, %BASE_SPRINTF_FORMAT, @DIGIT_TO_CHAR, %CHAR_TO_DIGIT); Getopt::Long::Configure(qw(bundling no_ignore_case)); Getopt::Long::GetOptions(debug => \$debug); die("usage: $0 [ ...]\n") unless @ARGV >= 3; my($number, $from_base, @to_base) = @ARGV; # Convert number to base 10, as that is the base Perl expects. my $base10_number; if ($from_base == 10) { $base10_number = $number; } else { local $_ = $number; my @digits; while (1) { if (/\G \[(\d+)\]/gcx || /\G ([A-Za-z0-9])/gcx) { push(@digits, string_to_digit($1)); } elsif (/\G (.+)/) { warn("Trailing garbage \"$1\" in number.\n"); last; } else { last; } } $base10_number = 0; my $i = $#digits; foreach my $digit (@digits) { $base10_number += $CHAR_TO_DIGIT{uc $digit} * $from_base ** $i; $i--; } } printf( "original (base %3d): %s\n" . "original (base %3d): %s\n", $from_base, $number, 10, $base10_number, ); foreach my $to_base (@to_base) { printf( "\n" . "recursive (base %3d): %s\n" . "iterative (base %3d): %s\n", $to_base, recursive_convert_base($base10_number, $to_base), $to_base, iterative_convert_base($base10_number, $to_base), ); my $format = $BASE_SPRINTF_FORMAT{$to_base}; if ($format) { printf( "sprintf %s (base %3d): $format\n", $format, $to_base, $base10_number, ); } } ### SUBROUTINES ### BEGIN { @DIGIT_TO_CHAR = (0..9, 'A'..'Z'); %CHAR_TO_DIGIT = map { $DIGIT_TO_CHAR[$_], $_ } 0 .. $#DIGIT_TO_CHAR; %BASE_SPRINTF_FORMAT = ( 2 => '%b', 8 => '%o', 10 => '%d', 16 => '%X', ); } sub iterative_convert_base { my($number, $to_base) = @_; my @digits; while ($number) { my $d = int($number / $to_base); my $m = $number - $d * $to_base; push(@digits, digit_to_string($m)); $number = $d; } return join("", reverse @digits); } sub recursive_convert_base { my($number, $to_base) = @_; my $d = int($number / $to_base); my $m = $number - $d * $to_base; my $digit; $digit = recursive_convert_base($d, $to_base) if $d; $digit .= digit_to_string($m); return $digit; } sub digit_to_string { my($digit) = @_; if ($digit <= 36) { return $DIGIT_TO_CHAR[$digit]; } else { return "[$digit]"; } } sub string_to_digit { my($string) = @_; if (exists $CHAR_TO_DIGIT{$string}) { return $CHAR_TO_DIGIT{$string}; } else { return $string; } }