#!/usr/bin/perl # text2hex.pl # OpenEye Scientific Software # September 2001 # Autoflush STDOUT #STDOUT->autoflush(1); $| = 1; $argc = @ARGV; if( $argc == 2 ) { $filename = @ARGV[0]; $arrayname = @ARGV[1]; $pos1 = rindex($filename,"/") + 1; if ($pos1 > -1) { $tmpguard = substr($filename,$pos1); } else { $tmpguard = $filename; } $guard = uc($tmpguard); for ( $pos2 = rindex($guard,".") ; $pos2 > -1 ; $pos2 = rindex($guard,".") ) { $lnth = length($guard); $pos3 = $pos2 + 1; $pos4 = $lnth - $pos3; $tmpguard = $guard; $guard = substr($tmpguard,0,$pos2) . "_" . substr($tmpguard,$pos3,$pos4); } for ( $pos2 = rindex($guard,"-") ; $pos2 > -1 ; $pos2 = rindex($guard,"-") ) { $lnth = length($guard); $pos3 = $pos2 + 1; $pos4 = $lnth - $pos3; $tmpguard = $guard; $guard = substr($tmpguard,0,$pos2) . "_" . substr($tmpguard,$pos3,$pos4); } } elsif( $argc == 3 ) { $filename = @ARGV[0]; $arrayname = @ARGV[1]; $guard = @ARGV[2]; } else { print "usage: bin2hex.pl \n\n"; exit; } $debug = 0; open(F,$filename) || die "Error: Unable to open binary file!\n"; if( !$debug ) { print "#ifndef OE_" . $guard . "\n"; print "#define OE_" . $guard . "\n\n"; print "static const unsigned char " . $arrayname . "[] = {\n"; } binmode(F); $col = 0; $init = 0; $ignore = 0; $newline = 1; while( !eof(F) ) { $ch = ord(getc(F)); if( $ch == 13 ) { $ch = 0; } if( $ignore ) { if( $ch == 10 ) { $ignore = 0; } $ch = 0; } elsif( $newline ) { if ( $ch == 35 ) { $ignore = 1; $ch = 0; } elsif( $ch ) { $newline = 0; } } elsif( $ch == 10 ) { $newline = 1; } if( $ch ) { if( $debug ) { print chr($ch); } else { if( $init ) { print ","; } else { $init = 1; } if( $col >= 15 ) { print "\n"; $col = 0; } print sprintf("0x%02X",$ch); $col++; } } } if( !$debug ) { if( $col >= 15 ) { print ",\n0x00};\n\n"; } else { print ",0x00};\n\n"; } print "#endif // OE_" . $guard . "\n\n"; } close(F); exit;