home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 27 / IOPROG_27.ISO / SOFT / PERLPAD3.ZIP / PERLPAD3.CAB / hex2bin.ppm < prev    next >
Encoding:
Text File  |  1999-02-06  |  1.8 KB  |  71 lines

  1. Hex2Bin
  2. Author:bekman Stas
  3. #!/usr/local/bin/perl -s
  4. #
  5. # hex2bin.pl
  6. #
  7. # This script converts hex number(s) to binary. and prints out each digit xxxx.
  8. # You can ask to reverse bits and digits in the output
  9. # You can change the separate character by reassigning the $char variable!
  10.  
  11. # Run hex2bin.pl -h to see the options
  12. #
  13. # Last modified:    7/10/96
  14. # Author: Bekman Stas   <c0409532@techst02.technion.ac.il>
  15. #                       <sbekman@iil.intel.com>
  16.  
  17. $0 =~ s/^.*\///;        # cut the basename of the command
  18.  
  19. # Diagnostic part...
  20. if ($#ARGV<0 || $h) {
  21. die "\n\tConvert Hex Number(s) to Binary 
  22. \nUsage:
  23. \t $0 [-options] hex_number(s)
  24. options:
  25.    -h           You read me now
  26.    -no_revb    DON'T Reverse bits in binary number        
  27.    -no_revd    DON'T Reverse digits in hex number before conversion        
  28.    -no_verbose  Suppress verbose output
  29.     
  30. [Default settings]: REVERSE bits in binary number, REVERSE digits in hex number
  31. \n";
  32. }
  33.  
  34. # Get the numbers
  35. @hex=@ARGV;
  36.  
  37. # Define the separation character between each binary number
  38. $char=" ";
  39.  
  40. # Verbose output
  41.  
  42. if (!$no_verbose) {
  43.     ($no_revb) && (print "Normal Order of bits in binary number\n");
  44.     (!$no_revb) && (print "Reverse Order of bits in binary number\n"); 
  45.     ($no_revd) && (print "Normal Order of  digits in hex number before conversion\n"); 
  46.     (!$no_revd) && (print "Reverse Order of digits in hex number before conversion\n"); 
  47. }
  48.  
  49. foreach (@hex){
  50.     print "\n";
  51.     @digits=split //;
  52.  
  53.     if (!$no_revd) {
  54.     while ($digit=(pop @digits)) {hex2bin();}
  55.     } else {
  56.     while ($digit=(shift @digits)) {hex2bin();}
  57.     }
  58.     print "\n";
  59. }
  60.  
  61. sub hex2bin {
  62.     if (!$no_revb) {
  63.     print substr (unpack ("b8",pack("H",$digit)),-4);
  64.     print "$char";
  65.     } else {
  66.     print unpack ("B4",pack("H",$digit));
  67.     print "$char";
  68.     }
  69. }
  70.  
  71.