home *** CD-ROM | disk | FTP | other *** search
- Hex2Bin
- Author:bekman Stas
- #!/usr/local/bin/perl -s
- #
- # hex2bin.pl
- #
- # This script converts hex number(s) to binary. and prints out each digit xxxx.
- # You can ask to reverse bits and digits in the output
- # You can change the separate character by reassigning the $char variable!
-
- # Run hex2bin.pl -h to see the options
- #
- # Last modified: 7/10/96
- # Author: Bekman Stas <c0409532@techst02.technion.ac.il>
- # <sbekman@iil.intel.com>
-
- $0 =~ s/^.*\///; # cut the basename of the command
-
- # Diagnostic part...
- if ($#ARGV<0 || $h) {
- die "\n\tConvert Hex Number(s) to Binary
- \nUsage:
- \t $0 [-options] hex_number(s)
- options:
- -h You read me now
- -no_revb DON'T Reverse bits in binary number
- -no_revd DON'T Reverse digits in hex number before conversion
- -no_verbose Suppress verbose output
-
- [Default settings]: REVERSE bits in binary number, REVERSE digits in hex number
- \n";
- }
-
- # Get the numbers
- @hex=@ARGV;
-
- # Define the separation character between each binary number
- $char=" ";
-
- # Verbose output
-
- if (!$no_verbose) {
- ($no_revb) && (print "Normal Order of bits in binary number\n");
- (!$no_revb) && (print "Reverse Order of bits in binary number\n");
- ($no_revd) && (print "Normal Order of digits in hex number before conversion\n");
- (!$no_revd) && (print "Reverse Order of digits in hex number before conversion\n");
- }
-
- foreach (@hex){
- print "\n";
- @digits=split //;
-
- if (!$no_revd) {
- while ($digit=(pop @digits)) {hex2bin();}
- } else {
- while ($digit=(shift @digits)) {hex2bin();}
- }
- print "\n";
- }
-
- sub hex2bin {
- if (!$no_revb) {
- print substr (unpack ("b8",pack("H",$digit)),-4);
- print "$char";
- } else {
- print unpack ("B4",pack("H",$digit));
- print "$char";
- }
- }
-
-