<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">#!/usr/bin/perl
#
# bin2c [-s] variablename
#
# Example:
#
# bash$ cat a.out | bin2c my_aout_array &gt; datafile.h
#
# bin2c converts a binary file to a character array for use with gcc.
# The -s flag will cause it to swap bytes ( EFBE -&gt; BEEF ).
#
# David Goldsmith
# &lt;dhg@dec.net&gt;
# http://www.dec.net/~dhg
#
# $Log: bin2c,v $
# Revision 1.3  1998/01/16 02:44:58  dhg
# *** empty log message ***
#
# Revision 1.2  1998/01/14 23:40:57  dhg
# uses getopts
#
# Revision 1.1  1998/01/14 22:55:58  dhg
# Initial revision
#
#

require "getopts.pl";

&amp;Getopts('c:s');

# assuming static.
print "static char " . shift( @ARGV ) . "[]=" ;

# have to add a count variable so we can dump x bytes of a
# binary to the array.
while( ( $len = read( STDIN, $data, 2 ) ) == 2 )
{
   print "\n\"" if !$counter;
   @temp = unpack( 'C2', $data );
   if ( !$opt_s )
   {
      $foo = sprintf( '\x%2.2x\x%2.2x', @temp );
   }
   else
   {
      $foo = sprintf( '\x%2.2x\x%2.2x',	@temp[1], @temp[0] );
   }
   print $foo;
   $counter++;
   if ( $counter == 9 )
   {
      $counter = 0;
      print '"';
   } 
}
# This will happen if the file has an odd number of bytes
if ( $len ) 
{
   $temp = unpack( 'C', $data );
   $foo = sprintf( '\x%2.2x', $temp );
   print $foo;
}
print '";' . "\n" ;

</pre></body></html>