Skip to content

Instantly share code, notes, and snippets.

@arms22
Last active March 23, 2019 00:59
Show Gist options
  • Select an option

  • Save arms22/5931222 to your computer and use it in GitHub Desktop.

Select an option

Save arms22/5931222 to your computer and use it in GitHub Desktop.
convert binary into c sourcecode
$pat = ARGV
# usage:
# ruby bin2c.rb symbol input > output
sym = $pat[0]
fin = open $pat[1], "rb"
printf "\n\nconst unsigned char %s[] = {\n", sym
while(d = fin.read(16))
d.each_byte.with_index do |byte, i|
if (i % 4) == 0 and i > 0
printf " "
end
# printf "%d,", byte
printf "0x%02x,", byte
end
printf "\n"
end
printf "};\n\n"
printf "const size_t %1$s_size = sizeof( %1$s );\n", sym
fin.close
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment