Advertisement
Guest User

binarystring

a guest
Apr 24th, 2015
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.38 KB | None | 0 0
  1. local inputs = {...}
  2.    
  3. list = {
  4.     [0] = "0",
  5.     [1] = "1",
  6.     [2] = "2",
  7.     [3] = "3",
  8.     [4] = "4",
  9.     [5] = "5",
  10.     [6] = "6",
  11.     [7] = "7",
  12.     [8] = "8",
  13.     [9] = "9",
  14.     [10] = "A",
  15.     [11] = "B",
  16.     [12] = "C",
  17.     [13] = "D",
  18.     [14] = "E",
  19.     [15] = "F"
  20. }
  21.  
  22. function tostring(b)
  23.     s = ''
  24.     s = s .. list[math.floor(b/16)]
  25.     s = s .. list[b % 16]
  26.     return s;
  27. end
  28.  
  29. bytes = {
  30.     ['0'] = 0,
  31.     ['1'] = 1,
  32.     ["2"] = 2,
  33.     ["3"] = 3,
  34.     ["4"] = 4,
  35.     ["5"] = 5,
  36.     ["6"] = 6,
  37.     ["7"] = 7,
  38.     ["8"] = 8,
  39.     ["9"] = 9,
  40.     ["A"] = 10,
  41.     ["B"] = 11,
  42.     ['C'] = 12,
  43.     ["D"] = 13,
  44.     ["E"] = 14,
  45.     ["F"] = 15
  46. }
  47.  
  48. path = inputs[1]
  49.  
  50. if fs.exists(path) then
  51.     if inputs[2] == "pack" then
  52.  
  53.         file = fs.open(path, "rb")
  54.         str = ""
  55.         b = file.read()
  56.         i = 0
  57.         while b ~= nil do
  58.             str = str .. tostring(b)
  59.             b = file.read()
  60.             i = i + 1
  61.         end
  62.         file.close()
  63.         bytesLen = i-1
  64.    
  65.         file = fs.open("pack", "w")
  66.         file.writeLine(str)
  67.         file.close()
  68.     end
  69.     if inputs[2] == "unpack" then
  70.         file = fs.open(path, "r")
  71.         str = file.readLine(str)
  72.         file.close()
  73.    
  74.         file = fs.open("unpack", "wb")
  75.         i = 1
  76.         s = string.sub(str, i , i)
  77.         len = string.len(str)
  78.         while s ~= nil do
  79.             if i < len then
  80.             num = bytes[s]
  81.                 i = i + 1
  82.                 s =  string.sub(str, i , i)
  83.                 num = num*16 + bytes[s]
  84.                 file.write(num);
  85.                 i = i + 1
  86.                 s = string.sub(str, i , i)
  87.             else s = nil
  88.             end
  89.         end
  90.         file.close()
  91.     end
  92. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement