CoderJohn

Binary Functions - Computer Craft - Lua

Nov 15th, 2012
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.85 KB | None | 0 0
  1. function encryptNumber(number)
  2.   number = (number < 65536 and number >= 0 and number) or false;
  3.   if(not number)then return false; end
  4.   local whole = math.floor(number);
  5.   local decimal = number - whole;
  6.   local wholeBin = {};
  7.   for i = 1,16 do
  8.     decimal = decimal * 2;
  9.     wholeBin[16+i] = decimal-decimal%1;
  10.     decimal = decimal%1;
  11.   end
  12.   for i = 1,16 do
  13.     wholeBin[17-i] = whole%2;
  14.     whole = math.floor(whole/2);
  15.   end
  16.  return wholeBin;
  17. end
  18.  
  19. function decryptNumber(twholeBin)
  20.   local decimal,whole = 0,0;
  21.   for i = 32,17,-1 do
  22.     decimal = decimal + twholeBin[i]*2^(32-i);
  23.   end
  24.   for i = 16,1,-1 do
  25.     whole = whole + twholeBin[i]*2^(16-i);
  26.   end
  27.   return whole+decimal/65536;
  28. end
  29.  
  30. function binToString(twholeBin)
  31.   return table.concat(twholeBin,nil,1,16).."."..table.concat(twholeBin,nil,17):gsub("^0+",""):gsub("0+$","");
  32. end
Advertisement
Add Comment
Please, Sign In to add comment