local ToByte = function(String)
local FinishedString = ""
for i = 1, string.len(String) do
FinishedString..=string.byte(String, i, string.len(String)).." "
end
return FinishedString
end
print(ToByte("Rawr")) -->> 82 97 119 114
The problem you are having is brought on by the fact that the string.byte function only returns the ASCII value of the string’s first character. You must iterate over each character and obtain the ASCII value separately if you want to obtain the ASCII values of every character in the string.
local str = "Rawr"
local bytes = {}
for i = 1, str:len() do
local char = str:sub(i, i)
local byte = string.byte(char)
table.insert(bytes, byte)
end
print(str, bytes) -- "Rawr", {82, 97, 119, 114}
The string.byte method is used in this code to cycle through the characters in the str string and get their ASCII values. The ASCII values are then kept in a table called bytes.
The string can then be converted back to bytes using the string.char function by using this table of ASCII values. Here is an illustration of how to accomplish it:
local str = ""
for i, byte in ipairs(bytes) do
str = str .. string.char(byte)
end
print(str, bytes) -- "Rawr", {82, 97, 119, 114}
Using the string.char function, this code loops through the bytes table and changes each ASCII value back to a character. After that, it joins all the characters to create the original string.
I hope this is useful. If you have any further inquiries, please let me know.