Well, I've been doing so many things, when I was bored of course
So I want:
My code:
local abs, log, floor, insert, concat, format = math.abs, math.log, math.floor, table.insert, table.concat, string.format
function ToBinary(str)
log2, _result = log(2), {}
for k = 1, #str do
n, t, m = str:byte(k), {}, 0
m = log(n) / log2
if n < 0 then
m = m + 1 n = 2 ^ m - abs(n)
end
for e = floor(m + 1), 0, -1 do
t[#t + 1] = floor(n / 2 ^ e)
n = n % 2 ^ e
end
insert(_result, #concat(t) == 7 and format('0%s',concat(t)) or concat(t))
end
return concat(_result,'\32')
end
function FromBinary(str)
str, _result = str:gsub('\32',''),{''}
for k = 1, #str, 8 do
local byte = str:sub(k, k + 7)
assert(tonumber(byte,2), 'Malformed binary sequence, got: ' .. str:sub(k, #str))
insert(_result,string.char(tonumber(byte,2)))
end
return concat(_result)
end
The problem is that it works well, but when the character is over or under 8 symbols in binary code, it just wont decode the binary code, I'm talking about the unicode characters, tho I tried like detecting it and yea maybe someone will say just split the spaces but what if it has no spaces, and even ur eyes cant look into that and say, its 9 bytes or 7.
I hope there's someone who will understand me.