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

Converting Characters To Numbers

Started by PappleFruit, 26 July 2013 - 05:00 AM
PappleFruit #1
Posted 26 July 2013 - 07:00 AM
I'm trying to make a safe password program. It should let the input go through an algorithm, to convert it completely into numbers. The output should be sent via rednet to a different computer, which compares it with the right code. If they are equal, the comparing computer sends an OK-message to the computer, I want to access. If not, it sends a NOT-OK-message. The problem I'm having is converting the strings into numbers. I've already googled quite a lot but I could not find anything helpful.

Thanks for your help,
PappleFruit
Lyqyd #2
Posted 26 July 2013 - 12:39 PM
Split into new topic.

Look into string.byte and string.char.
PappleFruit #3
Posted 26 July 2013 - 01:22 PM
Thanks, but how can I get the number for each letter in a sentence? If I for example type in string.byte("Hello") it will just give me the number for H, not for the rest.
Symmetryc #4
Posted 26 July 2013 - 01:40 PM
Try doing something like this:

-- turn the string into a number
local str = "put your own string here"
local num = ""
for char in str:gmatch(".") do
  num = num.. ( ("0"):rep(3 - #tostring(string.byte(char))) .. string.byte(char) )
end

-- turn the number into a string
local str2 = ""
for char in num:gmatch("...") do
  str2 = str2 .. string.char(tonumber(char))
end
Note: It's actually a string that has number characters only for simplicity.
PappleFruit #5
Posted 26 July 2013 - 03:28 PM
Yeah, thanks for the code, totally did the trick for me. :)/>