#!/usr/bin/perl -w # # bases: Convert numbers to various bases and forms. # # Written: ?? By Steven J. DeRose, sderose@acm.org. # 2007-11-~~ sjd: Add -k, -t, -u, input multiplier letters. # # To do: # Handle UTF-8. # Break long numbers into groups of digits. use strict; use Getopt::Long; use Encode; my $version = "2008-07-01"; my $help = 0; my $humanUnits = 0; my $quiet = 0; my $time = 0; my $verbose = 0; # Process options Getopt::Long::Configure ("ignore_case"); my $result = GetOptions( "h|help|?" => \$help, "human|k" => \$humanUnits, "q|quiet!" => \$quiet, "t|time" => \$time, "v|verbose+" => \$verbose, "version" => sub { die "Version of $version, by Steven J. DeRose, sderose\@acm.org.\n"; } ); if ($help) { showUsage(); exit; } ($result) || die "Bad options.\n"; ($ARGV[0]) || die "No argument specified.\n"; my $k = 1000; my $m = 1000*1000; my $g = 1000*1000*1000; my $t = 1000*1000*1000*1000; my $p = 1000*1000*1000*1000*1000; my $K = 1024; my $M = 1024*1024; my $G = 1024*1024*1024; my $T = 1024*1024*1024*1024; my $P = 1024*1024*1024*1024*1024; ################################################################################ my $tot = shift; ($verbose) && print "Got '$tot'\n"; # See if user typed in a number with a suffix letter... $tot =~ m/\d+([KMGTP])$/i; if (defined $1) { my $unit = $1; if ($unit eq "k") { $tot *= $k; } elsif ($unit eq "m") { $tot *= $m; } elsif ($unit eq "g") { $tot *= $g; } elsif ($unit eq "t") { $tot *= $t; } elsif ($unit eq "p") { $tot *= $p; } elsif ($unit eq "K") { $tot *= $K; } elsif ($unit eq "M") { $tot *= $M; } elsif ($unit eq "G") { $tot *= $G; } elsif ($unit eq "T") { $tot *= $T; } elsif ($unit eq "P") { $tot *= $P; } } $tot = oct($tot) if ($tot =~ m/^0/); if ($time) { print timeCode($tot) . "\n"; } else { print sprintf(" o0%o, d%d, 0x%x, 0b%08b\n", $tot, $tot, $tot, $tot); if ($humanUnits && $tot>1023) { print sprintf( " Human-readable units: %4.2fK, %4.2fM, %4.2fG, %4.2fT\n", $tot/$K, $tot/$M, $tot/$G, $tot/$T); } if (!$quiet) { if ($tot > 31 && $tot < 128) { print " Printable character: '" . chr($tot) . "'\n"; } elsif ($tot >= 128 && $tot <= 0x2FFFF) { print " If that's a Unicode code point, the UTF-8 is(?): \n "; my @b = unpack("C*", chr($tot)); for (my $i=0; $i