S1xty
|
  |
| Joined: 25 Oct 2009 |
| Total Posts: 2712 |
|
|
| 27 Feb 2015 03:18 AM |
local module = {};
module.binInt = function(int)
local remainder = int; local bin = {}; local count = 8;
for i = 1, 8 do bin[i] = "0"; end
while remainder > 0 do if remainder - 2^count >= 0 then bin[8 - count] = "1"; remainder = remainder - 2^count; if remainder == 0 then return table.concat(bin); end count = count - 1; else count = count - 1; end end
end
module.toBinary = function(var) if type(var) == "number" then if var - math.floor(var) == 0 then return module.binInt(var); end elseif type(var) == "table" then local tab = {}; for i,v in pairs(var)do tab[i] = module.binInt(v); end return tab; else error("Error, module.toBinary. Table or integer expected, got " .. type(var)); end end
return module;
Basically to module.toBinary converts either an array of bytes or a single integer byte into a binary string or an array of binary strings. It could be useful for bitwise hashing functions. |
|
|
| Report Abuse |
|
|
| 27 Feb 2015 03:32 AM |
| I like hash. I needed a hash function a while ago, but not anymore :( |
|
|
| Report Abuse |
|