#!/usr/bin/perl
#
# ascii-decode.pl -- decode binary, decimal, octal or hexadecimal ascii codes to the appropriate characters
# written by spirit
#

###### settings ######

$quiet = 1;

###### functions ######

sub usage
{
	print "usage: ./ascii-decode.pl [<input_format>] <digits to decode>\n";
	print "         <input_format> may be one of the following :\n";
	print "         -d : decimal (default if omitted)\n";
	print "         -b : binary\n";
	print "         -o : octal\n";
	print "         -h : hex\n\n";
	exit 1;
}

sub intro
{
	print "ascii-decode.pl by spirit\n";
}

sub start
{
	print "starting output ...\n";
}

sub done
{
	print "...all done. exiting.\n";;
}

sub bin2dec 
{
    return unpack("N", pack("B32", substr("0" x 32 . shift, -32)));
}

###### main() ######


usage() unless(@ARGV >= 1);

&intro() unless $quiet;

$input_format = "-d";

if($ARGV[0] eq "-b" || $ARGV[0] eq "-h" || $ARGV[0] eq "-d" || $ARGV[0] eq "-o")
{
	$input_format = shift;
	
	if($input_format eq "-b")
	{
		print "treating input as binary digits\n" unless $quiet;
		foreach(@ARGV)
		{
			$_ = &bin2dec($_);
		}
	}

	if($input_format eq "-h")
	{
		print "treating input as hex digits\n" unless $quiet;
		foreach(@ARGV)
		{
			$_ = hex($_);
		}
	}

		
	if($input_format eq "-o")
	{
		print "treating input as octal digits\n" unless $quiet;
		foreach(@ARGV)
		{
			$_ = oct($_);
		}
	}
	
}
else
{
	print "treating input as decimal digits\n" unless $quiet;
}

@decdata = @ARGV;

&start() unless $quiet;

$out_string = "";

foreach(@decdata)
{
	print "$_ => " . chr($_) . "\n";
	$out_string = $out_string . chr($_);
}

print "\ncomplete string : \n" unless $quiet;
print "\n$out_string \n";


&done() unless $quiet;
exit 0;


