Skip to content

Instantly share code, notes, and snippets.

@polprog
Created January 1, 2026 16:43
Show Gist options
  • Select an option

  • Save polprog/7b161d4a6ba12ff6002f86362f869532 to your computer and use it in GitHub Desktop.

Select an option

Save polprog/7b161d4a6ba12ff6002f86362f869532 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
# bin2h - convert binary into a C array
use strict;
if($#ARGV < 0) {
print stderr "Usage: bin2h.pl input\n";
exit 1;
}
my $if = $ARGV[0];
my $ifh; # input and output file handles
#print "input file=".$if."\n";
#print "output file=".$of."\n";
open($ifh, "<", $if) or die $!;
my $n; my $hexnum;
my $i = 0;
my $headername = $if =~ s/[.-]/_/rg . "_h";
print "#ifndef __".uc($headername)."\n";
print "#define __".uc($headername)."\n";
print "// Genrated by bin2h.pl\n";
print "// input file: ".$if."\n";
# The data array
print "\nchar ".$if =~ s/[.-]/_/r . " = {\n ";
while($n = read($ifh, $hexnum, 1)){
printf "%02x, ", ord $hexnum;
$i++;
if ($i % 16 == 0){
print "\n ";
}
}
print "\n};\n\n";
print "int " . $if =~ s/[.-]/_/r . "_sz = " . $i . ";\n\n";
print "#endif //__".uc($headername)."\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment