theoriginalbit

ComputerCraft: File Hex Dump

Jan 13th, 2013
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.64 KB | None | 0 0
  1. function hex_dump( inFile, outFile )
  2.   if not fs.exists( inFile ) then error("Input file does not exist", 2) end
  3.  
  4.   local fInput = fs.open( inFile, "r" )
  5.   local text = fInput.readAll()
  6.   fInput.close()
  7.  
  8.  
  9.   local fOutput = fs.open( outFile, "w" )
  10.   local output = ""
  11.   for i = 1, #text, 16 do
  12.     local set = text:sub(i, i + 15)
  13.     output = output..string.format( '%08X  ', i - 1  )
  14.     set:gsub('.', function (c) output = output..string.format('%02X ',c:byte() ) end)
  15.     output = output..string.rep( ' ' ,3 * ( 16 - #set ) )
  16.     output = output..' '..set:gsub( '%c', '.' ).."\n"
  17.   end
  18.  
  19.   fOutput.write(output)
  20.   fOutput.close()
  21. end
Add Comment
Please, Sign In to add comment