I am trying to make a lua script that takes an input of numbers seperated by commas, and turns them into letters, so 1 = a ect, however I have not found a way to do this easily because the string libray outputs a = 97, so I have no clue where to go now, any help?
You can use string.byte and string.char functions:
string.char(97) == "a"
string.byte("a") == 97
If you want to start from "a" (97), then just subtract that number:
local function ord(char)
return string.byte(char)-string.byte("a")+1
end
This will return 1 for "a", 2 for "b" and so on. You can make it handle "A", "B" and others in a similar way.
If you need number-to-char, then something like this may work:
local function char(num)
return string.char(string.byte("a")+num-1)
end
Merely just account for the starting value of a-z in the ascii table.
function convert(...)
local ar = {...}
local con = {}
for i,v in pairs(ar) do
table.insert(con, ("").char(v+96))
end
return con;
end
for i,v in pairs(convert(1,2,3,4)) do
print(v)
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With