Skip to content

Instantly share code, notes, and snippets.

@carstene1ns
Last active June 5, 2018 00:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carstene1ns/666a7bca1c9879da6dca to your computer and use it in GitHub Desktop.
Save carstene1ns/666a7bca1c9879da6dca to your computer and use it in GitHub Desktop.
`xxd -i` creates header files with hexdumps of any raw file. This simple ruby script saves the content of the header file in raw form.
#!/usr/bin/env ruby
# read
xxd = IO.read('file.xxd')
# values are comma separated
chars = xxd.split(',')
# with whitespaces
chars.map! {|c| c.strip}
# in hex
chars.map! {|c| c.to_i(16)}
# put back together
xxd = chars.pack('c*')
# write
File.open('file.raw','w') {|f| f.write xxd}
unsigned char hello_world_txt[] = {
0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x57, 0x4f, 0x52, 0x4c, 0x44, 0x21
};
unsigned int hello_world_txt_len = 12;
0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x57, 0x4f, 0x52, 0x4c, 0x44, 0x21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment