hex2bin.pl
上传用户:caisangzi8
上传日期:2013-10-25
资源大小:15756k
文件大小:2k
源码类别:

DVD

开发平台:

C/C++

  1. #! /home/potatooo/xcc/bin/perl
  2. #
  3. # hex2bin.pl
  4. #
  5. # transform a hex (or what-ever) file into binary.
  6. #
  7. $opt_bigendian = 1;
  8. $opt_short = 0;
  9. $opt_byte = 0;
  10. for ($argvcnt=0; $argvcnt<@ARGV && $ARGV[$argvcnt] =~ /^-(.*)/; $argvcnt++)
  11. {
  12.   if ($ARGV[$argvcnt] =~ /^-z/)
  13.   {
  14.     $argvcnt++;
  15.     last;
  16.   }
  17.   if ($ARGV[$argvcnt] =~ /^--help/
  18.       || $ARGV[$argvcnt] =~ /^-hz/)
  19.   {
  20.     print("
  21. Usage: $0 [options] [inputfile [outputfile]]
  22. where option is:
  23.   --little-endian   Specify little-endian output
  24.   --big-endian      Specify big-endian output (default)
  25.   --byte            Specify 8-bit output
  26.   --short           Specify 16-bit output
  27.   --long            Specify 32-bit output (default)
  28. ");
  29.     exit(-1);
  30.   } 
  31.   elsif ($ARGV[$argvcnt] =~ /^--little-endian/)
  32.   {
  33.     $opt_bigendian = 0;
  34.   } 
  35.   elsif ($ARGV[$argvcnt] =~ /^--big-endian/)
  36.   {
  37.     $opt_bigendian = 1;
  38.   } 
  39.   elsif ($ARGV[$argvcnt] =~ /^--short/)
  40.   {
  41.     $opt_short = 1;
  42.     $opt_byte = 0;
  43.   }
  44.    elsif ($ARGV[$argvcnt] =~ /^--byte/)
  45.   {
  46.     $opt_short = 0;
  47.     $opt_byte = 1;
  48.   } 
  49.   elsif ($ARGV[$argvcnt] =~ /^--long/)
  50.   {
  51.     $opt_short = 0;
  52.     $opt_byte = 0;
  53.   }
  54.   else {
  55.     die "$0: illegal option $ARGV[$argvcnt]";
  56.   }
  57. }
  58. if ($argvcnt<@ARGV) {
  59.   open(STDIN, "<$ARGV[$argvcnt]") || die "$0: err: can't open $ARGV[$argvcnt]";
  60.   $argvcnt++;
  61. }
  62. if ($argvcnt<@ARGV) {
  63.   open(STDOUT, ">$ARGV[$argvcnt]") || die "$0: err: can't open $ARGV[$argvcnt]";
  64.   $argvcnt++;
  65. }
  66. #set output to binary mode
  67. binmode STDOUT;
  68. $spec = ($opt_bigendian) ? 
  69.           (($opt_byte) ? "C" : (($opt_short) ? "n" : "N")) 
  70.         : (($opt_byte) ? "C" : (($opt_short) ? "v" : "V")) ;
  71. while (<STDIN>)
  72. {
  73.   chop($_);
  74.   $val = hex($_);
  75.   print STDERR "$0: warn: value $_ exceed limitn" if ($val>255 && $opt_byte);
  76.   print STDERR "$0: warn: value $_ exceed limitn" if ($val>65535 && $opt_short);
  77.   print STDOUT pack($spec, hex($_))  if ($_ ne "");
  78. }
  79. close(STDIN);
  80. close(STDOUT);