Hi!
I’m trying to convert a string into binary, but I’m unsure about how I could do it.
Can someone give me an explanation that is applicable to lua, please?
Thank you!
I’ll assume you want to turn your string into a byte array, its worth noting Lua internally will not represent a number as binary, but this code should work.
local convertStringToByteArray(str)
local byteArray = {}
for i = 1, #str do
local c = string.sub(str, i, i)
table.insert(byteArray, string.byte(c))
end
return byteArray
end
I don’t want to convert the string into a byte array, I want to convert it into binary.
not going to give you a full script, but I have useful resources for you!
first step is to get each letter using a for loop and getting the ascii code of each character using string.byte
for Character in string.gmatch("Hello", ".") do
print(string.byte(Character))
end
then you can use this link to find many functions that convert decimal to binary Lua: print integer as a binary - Stack Overflow
function StringToBinary(String)
local BinaryString = ""
for Character in string.gmatch(String, ".") do
BinaryString ..= DecimalToBinary(string.byte(Character)).." "
end
return BinaryString:sub(1, -2)
end
print(StringToBinary("Hello"))
DecimalToBinary would be a function you picked from that link, which would convert the ascii code to binary
hopefully this helps!
Thanks!
I found an explanation online and wrote a function to convert numbers into binary.
So to convert a text I used gmatch:
for letter in string.gmatch("Hello", ".") do
local byte = letter:byte(1, 1)
if got then
got ..= convert(byte) .. " "
else
got = convert(byte)
end
end
print(got) --> 10010001100101 1101100 1101100 1101111
But what it outputs for “Hello” is apparently wrong.
You’ll find this helpful, as it’s able to convert strings to binary using string.format
, vice versa.
local function NumberString
(Number)
local String = ''
repeat
local Remainder = Number % 2
String = Remainder .. String
Number = (Number - Remainder) / 2
until Number == 0
return String
end
-- // Why not convert binary into a string as well? It's a cool trick.
function FromBinary
(String)
if (#String % 8 ~= 0)
then
error('Malformed binary sequence', 2)
end
local Result = ''
for i = 1, (#String), 8 do
Result = Result..string.char(tonumber(String:sub(i, i + 7), 2))
end
return Result
end
local function ToBinary
(String)
if (#String > 0)
then
local Result = ''
for i = 1, (#String)
do
Result = Result .. string.format('%08d', NumberString(string.byte(string.sub(String, i, i))))
end
return Result
else
return nil
end
end
print(FromBinary('011010000110010101101100011011000110111100100000011101110110111101110010011011000110010000100001'))
-- // hello world!
print(ToBinary('Hello world!'))
-- // 011010000110010101101100011011000110111100100000011101110110111101110010011011000110010000100001
Now I only have one problem:
Using what I wrote (What's an easy way to convert a string into binary? - #5 by GEILER123456), I get 10010001100101 1101100 1101100 1101111
from “Hello”, which is apparently wrong.
Thanks, I saw that on github but wanted to write my own version.
There’s one thing left to fix though: What's an easy way to convert a string into binary? - #8 by GEILER123456
This was not on GitHub.
Also, having a space or not does not affect the output.
My code works pretty well and I use it in a few projects already.
You are welcome.
Here’s an example: Paste the binary from my script into this tool:
I managed to fix this by adding a “0” to the beginning of each series.
for letter in string.gmatch("Hello", ".") do
local byte = letter:byte()
got ..= "0" .. convert(byte) .. " "
end
Thank you all for the help!
actually nevermind, I realized that some converters add a “0” in the beginning of characters