The encoded string to binary value for “The boogie man was here.” would be: 01010100 01101000 01100101 00100000 01100010 01101111 01101111 01100111 01101001 01100101 00100000 01101101 01100001 01101110 00100000 01110111 01100001 01110011 00100000 01101000 01100101 01110010 01100101
However, If you want to make a function where you can input any string value and it returns back the binary value, here’s a function that does just that:
function stringToBinary(inputString)
local binaryString = ""
for i = 1, #inputString do
local char = inputString:sub(i, i)
local ascii = string.byte(char)
local binary = ""
while ascii > 0 do
local bit = ascii % 2
binary = bit .. binary
ascii = math.floor(ascii / 2)
end
while #binary < 8 do
binary = "0" .. binary
end
binaryString = binaryString .. binary .. " "
end
return binaryString:sub(1, -2)
end
-- example
local input = "a"
local binaryOutput = stringToBinary(input)
print(binaryOutput) --> 01100001
If you find this helpful, I’d appreciate if you solution it
@capsori’s solution is flawless, but I’d like to share a shorter solution that I was using in my projects a long time ago
local function stringToBinary(str)
return str:gsub(".", function(character)
local result = ""
for bit_index = 7, 0, -1 do
result ..= bit32.extract(character:byte(), bit_index) -- extract will return the bit at the (bit_index+1)the position
end
return result .. " "
end):sub(1, -2) -- to remove the extra space at the end
end
I didn’t test the code myself, so I don’t know if it truly works, but in theory it should.