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 ?
This is a read-only snapshot of the ComputerCraft forums,
taken in April 2020.
[Lua] Try to convert letters to numbers [solved]
Started by bjornir90, 10 November 2012 - 10:45 AMPosted 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 ?
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 ?
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
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:
For more info see here:
Roblox - String Manipulation
Lua PIL - string.byte
Lua PIL - 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
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 ?
I don't understand the line with "char = input:sub(i,i)" What this do exactly ?
Posted 10 November 2012 - 12:11 PM
Thanks you I don't see that before I reply :unsure:/>/>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
Posted 10 November 2012 - 01:18 PM
Is the string.byte needs a maj or any type of letter please ?
Posted 10 November 2012 - 01:52 PM
What do you mean with "maj"?
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
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
Posted 10 November 2012 - 10:49 PM
Sorry it's a french word –' I mean capital letter (yeah, google translation sorry)What do you mean with "maj"?
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:/>/>
Posted 11 November 2012 - 02:02 AM
Oh, alright, no problem. :unsure:/>/>Sorry it's a french word –' I mean capital letter (yeah, google translation sorry)
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. ^_^/>/>
Posted 11 November 2012 - 05:52 AM
Thanks you !! I saw that warning but as I want to use it only in computercraft it do not matter :(/>/>Oh, alright, no problem. :unsure:/>/>Sorry it's a french word –' I mean capital letter (yeah, google translation sorry)
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. ^_^/>/>