Binary Encode/Decode?

Is there a way I can encode/decode characters to binary? What I mean is a way so I can encode it like:

Hello I am an example

Would be:

01001000 01100101 01101100 01101100 01101111 00100000 01001001 00100000 01100001 01101101 00100000 01100001 01101110 00100000 01100101 01111000 01100001 01101101 01110000 01101100 01100101

there’s a topic for this:

First you would have to convert the binary into base 10. There is actually a built-in function that allows for this: tonumber()
The second parameter allows you to input the base.
Then you can use string.char() which allows you to input multiple numerical values and returns a string.
You will also have to convert the bytes into a table and iterate through them

the code should be this

local binary = "01001000 01100101 01101100 01101100 01101111 00100000 01001001 00100000 01100001 01101101 00100000 01100001 01101110 00100000 01100101 01111000 01100001 01101101 01110000 01101100 01100101"
-- has spaces in between the bytes
local str = "" -- will fill this in the loop
for _, Byte in ipairs(binary:split(" ")) do -- split using ' '
    str ..= string.char(tonumber(Byte, 2)) -- convert to base10, get char, and add to 'str'
end
print(str) -- should give the correct result

Although string.char() can take in multiple

1 Like
local function StringToBinary(String)
	local BinaryString = {}
	
	for i, Character in ipairs(String:split'') do --> ex: {"A", "B", "C"}
		local Binary = ""
		local Byte = Character:byte() -- convert character to ascii character code
		while Byte > 0 do
			-- apply formula for converting number to binary
			Binary = tostring(Byte % 2) .. Binary
			Byte = math.modf(Byte / 2) -- modf to strip decimal
		end
		table.insert(BinaryString, string.format("%.8d", Binary)) -- format string to always have at least 8 characters (00000000)
	end
	
	return table.concat(BinaryString, " ")
end

local function BinaryToString(BinaryString)
	local String = ""
	
	for i, Binary in ipairs(BinaryString:split' ') do --> ex: {"01000001", "01000010", "01000011"}
		local Byte = tonumber(Binary, 2) -- convert binary (base 2) to ascii character code
		String ..= string.char(Byte) -- get character from ascii code and append it at end of string
	end
	
	return String
end

local BinaryString = StringToBinary'Hello I am an example 1234567890 ABC XYZ ~!@#$%^&*()_+'
print(BinaryString)
--> 01001000 01100101 01101100 01101100 01101111 00100000 01001001 00100000 01100001 01101101 00100000 01100001 01101110 00100000 01100101 01111000 01100001 01101101 01110000 01101100 01100101 00100000 00110001 00110010 00110011 00110100 00110101 00110110 00110111 00111000 00111001 00110000 00100000 01000001 01000010 01000011 00100000 01011000 01011001 01011010 00100000 01111110 00100001 01000000 00100011 00100100 00100101 01011110 00100110 00101010 00101000 00101001 01011111 00101011

local String = BinaryToString(BinaryString)
print(String)
--> Hello I am an example 1234567890 ABC XYZ ~!@#$%^&*()_+

-- your example
print(BinaryToString'01001000 01100101 01101100 01101100 01101111 00100000 01001001 00100000 01100001 01101101 00100000 01100001 01101110 00100000 01100101 01111000 01100001 01101101 01110000 01101100 01100101')
--> Hello I am an example
8 Likes