Skip to content

Instantly share code, notes, and snippets.

@techbelly
Created March 16, 2012 20:22
Show Gist options
  • Save techbelly/2052408 to your computer and use it in GitHub Desktop.
Save techbelly/2052408 to your computer and use it in GitHub Desktop.
def encode(arr)
last_byte = nil
count = 1
(arr + [nil]).each do |byte|
if byte == last_byte && count < 255
count = count + 1
else
yield [count,last_byte] if last_byte
last_byte = byte
count = 1
end
end
end
f = File.open("thing","rb")
bytes = f.read
puts "byte byteArray[] = {\n"
column = 0
encode(bytes.bytes.to_a) { |count,byte|
print "0x%x, " % count
print "0x%x, " % byte
column += 1
if column % 20 == 0
puts "\n"
end
}
puts "\n};"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment