-
-
Save mr-salty/c225dadbfd5708f6ef7347b294f56527 to your computer and use it in GitHub Desktop.
script to convert numbers/expressions to hex/oct/dec/binary
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/perl | |
sub Usage { | |
$0 =~ s#.*/##g; | |
die <<EOM; | |
Usage: $0 [-hodsbn] expr [...] | |
expr may contain decimal/hex/octal numbers and operators, unless expr | |
is suffixed with b, in which case it is interpreted as a binary number | |
and operators are not allowed. | |
Converts to hex, octal, signed decimal, unsigned decimal, and binary, | |
and also indicates which bits are set. Flags may be used to turn off | |
specific conversions: | |
h = hex, o = oct, d = dec, s = signed dec, b = binary, n = set bits | |
EOM | |
} | |
use Getopt::Std; | |
Usage() unless (getopts("hodsbn")); | |
Usage() if ($#ARGV == -1); | |
foreach $arg (@ARGV) { | |
if ($arg =~ /^[01]*b$/) { | |
# convert from binary. | |
$num = 0; | |
$idx = 0; | |
chop $arg; | |
while ($arg ne "") | |
{ | |
$bit = chop $arg; | |
last if (($bit ne "0") && ($bit ne "1")); | |
$num |= (1 << $idx) if ($bit eq "1"); | |
++$idx; | |
} | |
} else { | |
# TODO this can produce perl error messages on the tty; it might be | |
# nice to be a little cleaner about it. | |
$num = eval "$arg"; | |
} | |
printf "0x%08x ", $num unless $opt_h; | |
printf "%012o ", $num unless $opt_o; | |
printf "%10u ", $num unless $opt_d; | |
printf "%11d ", $num unless $opt_s; | |
# convert to binary | |
undef $bits; | |
for ($i = 31; $i >= 0; --$i) { | |
if ($num & (1 << $i)) { | |
$bits .= " $i"; | |
print "1" unless $opt_b; | |
} else { | |
print "0" unless $opt_b; | |
} | |
} | |
print "\n"; | |
print "set:$bits\n\n" unless $opt_n; | |
} |
TODO: I wrote this a long time ago and it assumes 32 bits, which affects 2 things (see the example for -100
above):
- binary conversion (and bits
set:
) only looks at the lowest 32 bits - the other conversions do use 64 bits on a 64 bit machine, but it throws off the column widths which were chosen to be wide enough for 32 bit numbers.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here's an example... the input can be decimal, hex, octal, binary (with
b
suffix), or an expression (expressions cannot contain binary)