Advertisement
Redxone

[CC] Paint to Binary

Jun 30th, 2016
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.88 KB | None | 0 0
  1. local ta = { ... }
  2. local img = ta[1]
  3. local to = ta[2]
  4.  
  5. local function getbin(baseten)
  6.     local bin = ""
  7.     local binkey = "01"
  8.     local rim
  9.     while baseten > 0 do
  10.        rim = math.floor(baseten%2)
  11.        bin = binkey:sub(rim+1,rim+1) .. bin
  12.        baseten = math.floor(baseten/2)
  13.     end
  14.     if(#bin < 4)then
  15.         bin = string.rep("0",4-#bin) .. bin
  16.     end
  17.     return bin
  18. end
  19.  
  20. local function imgtobin(img)
  21.     local f = fs.open(img,'r')
  22.     local cont = f.readAll()
  23.     local ndata = ""
  24.     f.close()
  25.     cont = cont:gsub(string.char(32),"")
  26.     for line in cont:gmatch("[^\n]+") do
  27.         for i=1, #line do
  28.             nline = tonumber(line:sub(i,i),16)
  29.             print(nline)
  30.             ndata = ndata .. " " .. getbin(nline)
  31.         end
  32.         ndata =  ndata .. "\n"
  33.     end
  34.     return ndata
  35. end
  36.  
  37. local data = imgtobin(img)
  38. local export = fs.open(to .. ".bin","w")
  39. export.write(data)
  40. export.close()
  41. print("Bin exported to: " .. to .. ".bin")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement