Discuss Scratch

Needaadoo
Scratcher
30 posts

How do I convert text to numbers then back?

I want to make a multiplayer game where you can convert text to number then back to numbers, but how?
Every time I do this it gives me a number like 13.
Here's the code I made to convert my text:

define convert text (txt)
set [characters] to [ABCDEFGHIJKLMNOPQRSTUVWSYZabcdefghijklmnopqrstuvwsyz1234567890!@#$%^&*()_-+={[]}:;"'<,>.?/|\~`]
set [next] to [10]
set [chat] to []
set [foo] to [1]
repeat (length of (txt))
if <(letter (foo) of [(characters)]) = (letter (foo) of (txt))> then
set [chat] to (join [(chat)] [next])
change [next] by (1)
change [foo] by (1)
end
end

Last edited by Needaadoo (Dec. 5, 2019 10:06:42)

deck26
Scratcher
1000+ posts

How do I convert text to numbers then back?

You need nested loops to encode. One loop finds a match for the current character and the second moves to the next character in the input.

If you use a list for the characters instead of a variable you can use the block introduced in Scratch 3 which will give you the item number of a value in a list which saves looping to search for it.

Powered by DjangoBB