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

[Lua] Try to convert letters to numbers [solved]

Started by bjornir90, 10 November 2012 - 10:45 AM
bjornir90 #1
Posted 10 November 2012 - 11:45 AM
Hi,
I want to take an user input wich is letters and i want to convert it to number to manipulate numbers instead of strings and do some calculation with it. How can I do that please ?
Kingdaro #2
Posted 10 November 2012 - 11:50 AM
Well, it depends on the scale of your numbers and letters. If you wanted 1 to 26 with a to z, you could just write out your numbers in a string, then go through the letters of the user's inputand get the position that the letters are in the string.


local letters = 'abcdefghijklmnopqrstuvwxyz'
local input = read() -- we'll say the user inputs "foo"

for i=1, #input do
  local char = input:sub(i,i) -- this gets the current letter of whatever number we are in the loop
  local num = letters:find(char) -- this returns the position of char in our letters string.

  print(num) -- for the purposes of this example, we'll just print the number.
end

-- if the user wrote "foo", it should come out as
-- 6
-- 15
-- 15
Espen #3
Posted 10 November 2012 - 12:09 PM
You want string.byte() and string.char().
The former converts a character to its numerical representation, the latter does the reverse.
Example:
string.byte("A")  -- Returns 65
string.char(65)  -- Returns 'A'

For more info see here:
Roblox - String Manipulation
Lua PIL - string.byte
Lua PIL - string.char
bjornir90 #4
Posted 10 November 2012 - 12:09 PM
Thanks you very much :unsure:/>/> But i have an other noob question :
I don't understand the line with "char = input:sub(i,i)" What this do exactly ?
bjornir90 #5
Posted 10 November 2012 - 12:11 PM
You want string.byte() and string.char().
The former converts a character to its numerical representation, the latter does the reverse.
Example:
string.byte("A")  -- Returns 65
string.char(65)  -- Returns 'A'

For more info see here:
Roblox - String Manipulation
Lua PIL - string.byte
Lua PIL - string.char
Thanks you I don't see that before I reply :unsure:/>/>
bjornir90 #6
Posted 10 November 2012 - 01:18 PM
Is the string.byte needs a maj or any type of letter please ?
Espen #7
Posted 10 November 2012 - 01:52 PM
What do you mean with "maj"?
callos #8
Posted 10 November 2012 - 02:11 PM
hey, really simple
if you want to change a string of letters (hereafter: word) into a number (5)

5 = word

if you want to type a word into the console and have a function return a number

a = 0
while a = 0 do
input = io.read()
if input == word then
– whatever you want, create and define 5 if you want
else
a = (a + 1)%2
end


hope that helps
bjornir90 #9
Posted 10 November 2012 - 10:49 PM
What do you mean with "maj"?
Sorry it's a french word –' I mean capital letter (yeah, google translation sorry)
bjornir90 #10
Posted 10 November 2012 - 10:53 PM
hey, really simple
if you want to change a string of letters (hereafter: word) into a number (5)

5 = word

if you want to type a word into the console and have a function return a number

a = 0
while a = 0 do
input = io.read()
if input == word then
– whatever you want, create and define 5 if you want
else
a = (a + 1)%2
end


hope that helps

Thanks you for your reply but what I want is something that convert each letter into a predefined number so I can retrieve it after on an other computer for example. :unsure:/>/>
Espen #11
Posted 11 November 2012 - 02:02 AM
Sorry it's a french word –' I mean capital letter (yeah, google translation sorry)
Oh, alright, no problem. :unsure:/>/>
No, you don't need capital letters. In fact you can use any characters, not just letters.
Ususally it will convert characters into their ASCII number.

There is a warning in the documentation though that "Numerical codes are not necessarily portable across platforms".
But as long as we're talking about the environment of ComputerCraft Lua I don't think this will be a problem at all. ^_^/>/>
bjornir90 #12
Posted 11 November 2012 - 05:52 AM
Sorry it's a french word –' I mean capital letter (yeah, google translation sorry)
Oh, alright, no problem. :unsure:/>/>
No, you don't need capital letters. In fact you can use any characters, not just letters.
Ususally it will convert characters into their ASCII number.

There is a warning in the documentation though that "Numerical codes are not necessarily portable across platforms".
But as long as we're talking about the environment of ComputerCraft Lua I don't think this will be a problem at all. ^_^/>/>
Thanks you !! I saw that warning but as I want to use it only in computercraft it do not matter :(/>/>