Originally only HLLs. Author's site:
http://www.chami.com/tips/delphi/052098D.html

I added support for the ASMs. If you try this on something large (eg: notepad.exe), it'll show nothing for a little while. Then you'll see output. I had to add REGEXs to correct it. Don't know why they slow it up.


bin2hex.pl
----------------------------------------------------------------
#
# bin2hex.pl by Chami.com
# http://www.chami.com/tips/
#
# 68k/x86 asm support added by Al Leitch

# number of characters per line
$chars_per_line = 15;

# -------------------------------------

# language id
#
# 0 = Perl (default)
# 1 = C / C++
# 2 = Pascal / Delphi
# 3 = x86 ASM
# 4 = 68k ASM

$lang  = $ARGV[1];

$rem_begin  = "begin binary data:";
$rem_end    = "end binary data.";

# initialize for Perl strings
# by default
$_var       = "# $rem_begin\n".
             "\$bin_data = # %d\n";
$_begin     = "\"";
$_end       = "\";\n";
$_break     = "\".\n\"";
$_format    = "\\x%02X";
$_separator = "";
$_comment   = "# $rem_end ".
             "size = %d bytes";


# C / C++
if(1 == $lang)
{
 $_var       = "/* $rem_begin */\n".
               "char bin_data[] = ".
               "/* %d */\n";
 $_begin     = "{";
 $_end       = "};\n";
 $_break     = "\n";
 $_format    = "0x%02X";
 $_separator = ",";
 $_comment   = "/* $rem_end ".
               "size = %d bytes */";
}
elsif(2 == $lang)
{
 $_var       = "{ $rem_begin }\n".
               "const bin_data : ".
               "array [1..%d] of ".
               "byte = \n";
 $_begin     = "(";
 $_end       = ");\n";
 $_break     = "\n";
 $_format    = "\$%02X";
 $_separator = ",";
 $_comment   = "{ $rem_end ".
               "size = %d bytes }";
}
# x86 asm
elsif(3 == $lang)
{
 $_var       = "".
               "BIN_DATA:\n";
 $_begin     = "db ";
 $_end       = "\n";
 $_break     = "\ndb";
 $_format    = "0%02Xh";
 $_separator = ",";
 $_comment   = "END_BIN_DATA:\n\n".
               "BIN_LEN EQU %d";
}
# 68k asm
elsif(4 == $lang)
{
 $_var       = "".
               "BIN_DATA:\n";
 $_begin     = "dc.b ";
 $_end       = "\n";
 $_break     = "\ndc.b";
 $_format    = "\$%02X";
 $_separator = ",";
 $_comment   = "END_BIN_DATA:\n\n".
               "BIN_LEN EQU %d";
}

if(open(F, "<".$ARGV[0]))
{
 binmode(F);

 $s = '';
 $i = 0;
 $count = 0;
 $first = 1;
 $s .= $_begin;
 while(!eof(F))
 {
   if($i >= $chars_per_line)
   {
     $s .= $_break;
     $i = 0;
   }
   if(!$first)
   {
     $s .= $_separator;
   }
   if ($lang == 3) {
   $s =~ s/\bdb,\b/db /; #x86 ASM
   }
   if ($lang == 4) {
   $s =~ s/\bdc\.b,\$\b/dc\.b \$/; #68K ASM
   }
   
   $s .= sprintf(
           $_format, ord(getc(F)));
   ++$i;
   ++$count;
   $first = 0;
 }
 $s .= $_end;
 $s .= sprintf $_comment, $count;
 $s .= "\n\n";

 $s = "\n".sprintf($_var, $count).$s;
   if ($lang == 3) {
   $s =~ s/\bdb,\b/db /; #x86 ASM
   }
   if ($lang == 4) {
   $s =~ s/\bdc\.b,\$\b/dc\.b \$/; #68K ASM
   }

 print $s;

 close( F );
}
else
{
 print
   "bin2hex.pl by Chami.com\n".
   "\n".
   "usage:\n".
   "  perl bin2hex.pl <binary file>".
   " <language id>\n".
   "\n".
   "  <binary file> : path to the ".
   "binary file\n".
   "  <language id> : 0 = Perl, ".
   "1 = C/C++/Java, ".
   "2 = Pascal/Delphi\n".
   "\t\t  3 = x86 ASM, ".
   "4 = 68k ASM\n";
}
Posted on 2009-04-15 00:22:24 by Al_Leitch
To clear possible misconception, I posted this merely as a useful utility. Being an ASM forum, I'm not trying to get help on this script.
Posted on 2009-04-15 13:08:43 by Al_Leitch