Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How do I generate random hexadecimal numbers for characters?

Asked by 6 years ago

I want to generate a random character by using hexadecimal codes (\65-90). However, whenever I use "\", Roblox says everything after is an unfinished string. If I use "\" + (lets say) 65, then it prints "\65" instead of "A". I use the second backslash to fix the error.

I'd rather not create a table with all the letters and iterate through because I find this to be more useful, given it's possible.

-- Here's what I have so far
-- This is broken because of the single backslash

local Code = {}
for i = 1, 10 do
    table.insert(Code, "\" ..tostring(math.random(65, 90)))
    print(Code[i])
end

Any pointers would be greatly appreciated, thank you!

1 answer

Log in to vote
0
Answered by 6 years ago

So from investigating with the lua interpreter a bit, I'm not sure that you can concatenate a string escape sequence in a lua string. Luckily there is a workaround for converting from ASCII codes like you want into characters in string.char. Just to be specific about terminology, I don't believe that these characters are represented in hexadecimal because I could not find that language anywhere. Hexidecimal is the base 16 representation of numbers.

local Code = {}
for i = 1, 10 do
    table.insert(Code, string.char(math.random(65, 90)))
    print(Code[i])
end
0
That right there is exactly what I needed! Thank you very much! Julian_Orteil 48 — 6y
Ad

Answer this question