Created
July 7, 2013 14:25
-
-
Save vaiorabbit/5943635 to your computer and use it in GitHub Desktop.
Converts binary file into C array.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # | |
| # usage : $ ruby bin2c.rb loremipsum.txt.bin | |
| # generates 'loremipsum_txt_bin.c' : | |
| # | |
| # char loremipsum_txt_bin_array[/*15716*/] = { | |
| # 0x5a,0x50,0x46,0x4c,0x01,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x00, | |
| # ... | |
| # | |
| if __FILE__ == $0 | |
| begin | |
| fn_base = ARGV[0].gsub(/[(){}\[\]\<\>\!\~\$\=\+\-\.]/, "_") | |
| ARGF.set_encoding('BINARY') | |
| f_in = ARGF.read | |
| File.open( "#{fn_base}.c", "w:ASCII-8BIT" ) do |f_out| | |
| f_out.puts "char #{fn_base}_array[/*#{f_in.length}*/] = {" | |
| index = 0 | |
| f_in.each_byte do |byte| | |
| f_out.printf "0x%02x,", byte | |
| index += 1 | |
| f_out.print "\n" if index % 16 == 0 | |
| end | |
| f_out.puts "#{"\n" if index % 16 != 0}};" | |
| end | |
| rescue Exception => e | |
| p e | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment