eval '(exit $?0)' && eval 'exec perl -w -S $0 ${1+"$@"}' && eval 'exec perl -w -S $0 $argv:q' if 0; # The above invocation finds perl in the path, wherever it may be use strict; use warnings; use Math::BigInt; # encode/decode ID numbers our $DECODE_OPT = q/-d/; our $ENCODE_OPT = q/-e/; sub usage { warn "@_\n\n"; warn "usage: $0 [$DECODE_OPT|$ENCODE_OPT] arg arg...\n\n"; warn "\t-e\tencode an ID number into binary/octal/hex/decimal\n"; warn "\t-d\tdecode a binary/octal/hex/decimal into the equivalent text ID number\n\n"; warn "Notes:\n"; warn "\tvalues must be prefixed with by:\n"; warn "\t\tbinary 0b (\"zero bee\") (or 0B)\n"; warn "\t\toctal 0 (\"zero\")\n"; warn "\t\thex 0x (\"zero ex\") (or 0X)\n"; warn "\nExample: to encode N12345, use this command:\n"; warn "\t$0 $ENCODE_OPT N12345\n"; warn "\n"; die; } usage( "Error: No arguments supplied..." ) unless (@ARGV); usage( "Error: No encode/decode command used..." ) unless ($ARGV[0] =~ /^($ENCODE_OPT|$DECODE_OPT)$/i); my $opt = $1; shift @ARGV; usage( "Error: Not enough arguments supplied..." ) unless (@ARGV); # use Math::BigInt for arbitrary precision math (32bit integers too small) our $ZERO = new Math::BigInt +0; our %encode; @encode{" ","A".."Z","a".."z","0".."9"} = (0,1..26,1..26,27..36); our %decode; @decode{0..36} = (" ","A".."Z",0..9); our $base = $ZERO + scalar keys %decode; sub encode { my $x = shift; my @x = split '', $x; my $r = $ZERO; for my $c ( @x ) { $r = $r * 37 + $encode{$c}; } return $r; } sub decode { my $x = shift; return "invalid decode" if $x =~ /[a-z]/i; my $r = ''; while ( $x ) { my $LSD = $x % $base; # least significant digit $r = $decode{$LSD} . $r; #prepend $x = ($x - $LSD)/$base; } return $r; } for (@ARGV) { if ( $opt =~ /^$ENCODE_OPT$/i ) { # encode my $e = $ZERO + encode($_); printf "Text: %s\nBinary: %s\nOctal: %s\nHex: %s\nDecimal: %s\n\n", $_, $e->as_bin(), $e->as_oct(), $e->as_hex(), $e; } else { # decode printf "Input: %s\nText: %s\n\n", $_, decode($_ + $ZERO); } } exit;