Last active
August 29, 2015 14:08
-
-
Save mfadzilr/45043e14f4b9b6c7b1f5 to your computer and use it in GitHub Desktop.
Binary to hex
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
#!/usr/bin/env ruby | |
# Author : Muhamad Fadzil Ramli | |
# 25/10/2014 | |
# Binary to hexcode | |
# read bin file | |
fp = File.open(ARGV[0],"rb") | |
# read and convert to hex format | |
def readfile(fp) | |
file_data = fp.read.each_byte.map { |b| "%02x" % b } | |
return file_data | |
end | |
# add '\x' infront | |
def to_hex(data) | |
array = [] | |
data.map do |byte| | |
array << '\x' + byte | |
end | |
return array | |
end | |
# format the output to ruby style | |
def format(data) | |
lines = [] | |
data.each_slice(10) do |array| | |
lines << array.join | |
end | |
out = [] | |
res = lines.map do |line| | |
if line != lines.last | |
out << "\"" + line + "\" +\n" | |
else | |
out << "\"" + line + "\"\n" | |
end | |
end | |
return out | |
end | |
puts format(to_hex(readfile(fp))) | |
puts "" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment