theoriginalbit, on 20 August 2014 - 10:27 AM, said:
--snip--
Actually if you want to do entire strings it would be longer than a line... Example expanded for readability.
function fromBinary( str )
assert( #str % 8 == 0, "malformed binary sequence" )
local out = ""
for i = 1, #str, 8 do
local byte = str:sub(i, i + 8)
local char = tonumber(byte, 2)
out = out..string.char(char)
end
return out
end
Your code here is wrong and gave me some trouble

it should have local byte = str:sub(i,i + 7) since i already equals the first number, and you need 1-8 not 1-9
columna1, on 22 August 2014 - 04:40 AM, said:
I like the function you created to convert numbers to binary, very creative. However there are functions that do this quite well and (just as a tip) you would do well to learn some in order to cut down lines and add flexibility. Here is one:
digits = {} --# Note from Dragon: These three lines are useless as i can just set s to be remainder..s and have it work for binary, other bases, not so much, but i'm using it for binary.
for i=0,9 do digits[i] = strchar(strbyte('0')+i) end
for i=10,36 do digits[i] = strchar(strbyte('A')+i-10) end
function numberstring(number, base)
local s = ""
repeat
local remainder = mod(number,base)
s = digits[remainder]..s
number = (number-remainder)/base
until number==0
return s
end
This will convert to many bases and is used like numberstring(10,2) which will output "1010" which if needed you can append the needed ammount of 0s onto the end like so
str = numberstring(10,2)
str = string.repeat("0",8-#str)..str
--this would return "00001010"
--you can also build this into the function itself like so:
return string.repeat("0",8#s)..s
Remember these are just suggestions and you don't have to implement them if you don't want.

I like to learn new things so I gave you this, so have fun so please feel free to play around and edit it as you please!
I had trouble doing string.repeat as that doesn't seem to be an actual function that i can use. Also strchar and strbyte needed to be string.char and string.byte, i'm guessing you didn't care to fix it, and assumed i would fix it

Also mod isn't a function either, it is however an operator using % so that should be number % base. The rest worked amazingly

thank you for your help there, time to mess around
theoriginalbit, on 22 August 2014 - 05:10 AM, said:
-snip-
alternatively, using some Lua magic we can do this
str = string.format("%08d", numberString(10,2))
--snip--
the magic here is that even though
%d specifies number, Lua is able to treat the string as a number as it only contains numbers.
Your code here worked fabulously for using his numberstring function

I thank you both for your help and showing me some new things that i can attempt to use on the project i'm working on (its a secret

)
Edited by Dragon53535, 22 August 2014 - 11:19 PM.