This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
SethShadowrider's profile picture

Replacing letters in an input

Started by SethShadowrider, 22 June 2012 - 03:21 AM
SethShadowrider #1
Posted 22 June 2012 - 05:21 AM
I have a question, is it possible to read the letters in something a user inputted and change them? For example; say a user inputted "Hello" could the computer use variables set as something such as a = n b = o (rot13, I would use a complete set to translate every letter back and forth) and make a coder/decoder? So that "Hello" could be resent to the user as "Uryyb"?
kazagistar #2
Posted 22 June 2012 - 02:44 PM
Look up string.byte and string.char in the docs. Also this is like the fifth thread about this topic in a few days… what is going on?
tfoote #3
Posted 22 June 2012 - 02:55 PM
Check out this thread: I got a lot of help from these guys. http://www.computercraft.info/forums2/index.php?/topic/2209-encrypting/
MysticT #4
Posted 22 June 2012 - 03:59 PM
Well, it's really easy to do it with the string api and a table:

-- table with each character
local t = {
["a"] = "n",
["b"] = "o",
...
}
-- get the user input
local input = read()
local msg = ""
-- loop through every character
for char in string.gmatch(input, ".") do
  -- get the character
  local c = t[char] or char
  -- add it to the message
  msg = msg..c
end
-- now use msg for whatever you want
kazagistar #5
Posted 22 June 2012 - 04:08 PM
The parts you skip over with comments are significant. I am pretty sure byte and char are a better solution in almost every case.
MysticT #6
Posted 22 June 2012 - 04:39 PM
The parts you skip over with comments are significant. I am pretty sure byte and char are a better solution in almost every case.
How much more detail do you want? If you (or anyone else) want to know how the functions work, you can look at them in the lua manual.
Also, how would string.byte and string.char help here?