Discuss Scratch

AProductions
Scratcher
500+ posts

Converting usernames to numbers, please help

How could I convert a username into numbers for use in a variable? In other words how could I translate letters to numbers? Please help
RUBIXS
New Scratcher
60 posts

Converting usernames to numbers, please help

I'm not sure if it will work, but you could assign each letter a number a=1 b=2 and so on. then add the name into a list. split the list into separate lists, one for each letter. then use the numbers to convert it into one big number and combine that into a list or variable. Never tried it before but its worth a try
DadOfMrLog
Scratcher
1000+ posts

Converting usernames to numbers, please help

There are many examples of projects that encode strings into numbers - to use in cloud variables, for example. I suspect searching for ‘encode’/'encoding'/'encoder' will give you plenty of hits.

In my Cloud Var Ping Test project I have a couple of encoder/decoder blocks that do exactly what you want, so you could take a look there (and it has the useful extra feature that the encoder can tell apart upper & lower case - but note you need to have all the sprite's 52 costumes in with it for that to work - and it doesn't use any lists at all, instead doing it all via string manipulation…)

I didn't bother putting loads of ‘odd’ extra characters (like ¡€¢∞§¶•ªºœ∑…) into my set of allowed characters (just wanted to encode into two digits, so a set of only 100 characters allowed). But I've seen a couple of other encoders out there that try to include lots of extra possible characters, should you need them (though I think the basic principle of what I've done is probably straightforward enough for you to extend, anyway…)

Last edited by DadOfMrLog (June 28, 2013 02:58:27)

eastons
Scratcher
25 posts

Converting usernames to numbers, please help

Check out:
http://scratch.mit.edu/projects/11132259/

Converts username to a numeric string.

This example only uses alphabet letters and 0-9 but can be expanded by adding references to the alpha variable

eastons

Last edited by eastons (June 28, 2013 03:19:21)

greenflash
Scratcher
18 posts

Converting usernames to numbers, please help

RUBIXS wrote:

I'm not sure if it will work, but you could assign each letter a number a=1 b=2 and so on. then add the name into a list. split the list into separate lists, one for each letter. then use the numbers to convert it into one big number and combine that into a list or variable. Never tried it before but its worth a try

This general idea should work, but the only trouble is that for a number like “111” there are two possible interpretations– is is AAA? KA? AK?

The way around this would be to put a “0” in front of the one digit numbers, so A is 01… then you can see that 0111 is AK (01 and 11)
dracae
Scratcher
1000+ posts

Converting usernames to numbers, please help

greenflash wrote:

RUBIXS wrote:

I'm not sure if it will work, but you could assign each letter a number a=1 b=2 and so on. then add the name into a list. split the list into separate lists, one for each letter. then use the numbers to convert it into one big number and combine that into a list or variable. Never tried it before but its worth a try

This general idea should work, but the only trouble is that for a number like “111” there are two possible interpretations– is is AAA? KA? AK?

The way around this would be to put a “0” in front of the one digit numbers, so A is 01… then you can see that 0111 is AK (01 and 11)
Say if you had TA, with (T = 20) and (A = 1), then TA = 201!
But 201 can be (B = 2) and (A = 1) = BA!

Last edited by dracae (June 28, 2013 23:00:03)

kola2
Scratcher
100+ posts

Converting usernames to numbers, please help

Converting usernames to numbers is 100% possible, you just have to use 2 letter'd numbers like 01, 02, 03 all the way up to 99 (Or you can use 3 letter'd numbers, but 2 letter'd is alot more easy)

mitchboy
Scratcher
1000+ posts

Converting usernames to numbers, please help

dracae wrote:

greenflash wrote:

RUBIXS wrote:

I'm not sure if it will work, but you could assign each letter a number a=1 b=2 and so on. then add the name into a list. split the list into separate lists, one for each letter. then use the numbers to convert it into one big number and combine that into a list or variable. Never tried it before but its worth a try

This general idea should work, but the only trouble is that for a number like “111” there are two possible interpretations– is is AAA? KA? AK?

The way around this would be to put a “0” in front of the one digit numbers, so A is 01… then you can see that 0111 is AK (01 and 11)
Say if you had TA, with (T = 20) and (A = 1), then TA = 201!
But 201 can be (B = 2) and (A = 1) = BA!
Actually, TA would be 2001 and BA would be 0201.
dracae
Scratcher
1000+ posts

Converting usernames to numbers, please help

mitchboy wrote:

dracae wrote:

greenflash wrote:

RUBIXS wrote:

I'm not sure if it will work, but you could assign each letter a number a=1 b=2 and so on. then add the name into a list. split the list into separate lists, one for each letter. then use the numbers to convert it into one big number and combine that into a list or variable. Never tried it before but its worth a try

This general idea should work, but the only trouble is that for a number like “111” there are two possible interpretations– is is AAA? KA? AK?

The way around this would be to put a “0” in front of the one digit numbers, so A is 01… then you can see that 0111 is AK (01 and 11)
Say if you had TA, with (T = 20) and (A = 1), then TA = 201!
But 201 can be (B = 2) and (A = 1) = BA!
Actually, TA would be 2001 and BA would be 0201.
What system are YOU using?
DadOfMrLog
Scratcher
1000+ posts

Converting usernames to numbers, please help

Yeah, use leading zeros for letters that give numbers less than ten, so you always get two digits.

BUT… when you first get the digit you must from then on treat it ONLY as a string, NOT as a number (i.e. don't do maths on it) - otherwise it will ditch the leading zero again.

If you want to encode each letter into two digits, then you'll probably need to just about fill all 100 characters - meaning use “00” to “99” to refer to each character - so you will have to beware of the leading zero problem, as above.

However, if you go for even more than 100 characters, then you'd encode into three digits. You're unlikely to have 1000 characters, so you can just encode a character into three digits from “100” to “999” (so your first letter encodes as “100”), and that way don't have to worry about leading zeros.

Take your pick…

Last edited by DadOfMrLog (June 29, 2013 08:04:38)

mitchboy
Scratcher
1000+ posts

Converting usernames to numbers, please help

dracae wrote:

mitchboy wrote:

dracae wrote:

greenflash wrote:

This general idea should work, but the only trouble is that for a number like “111” there are two possible interpretations– is is AAA? KA? AK?

The way around this would be to put a “0” in front of the one digit numbers, so A is 01… then you can see that 0111 is AK (01 and 11)
Say if you had TA, with (T = 20) and (A = 1), then TA = 201!
But 201 can be (B = 2) and (A = 1) = BA!
Actually, TA would be 2001 and BA would be 0201.
What system are YOU using?
His system…

His system encodes A as 01, B as 02, T as 20, etc. It should then decode it the same way, so 0201 would decode as BA and 2001 would decode as TA.
dracae
Scratcher
1000+ posts

Converting usernames to numbers, please help

mitchboy wrote:

dracae wrote:

mitchboy wrote:

dracae wrote:

greenflash wrote:

This general idea should work, but the only trouble is that for a number like “111” there are two possible interpretations– is is AAA? KA? AK?

The way around this would be to put a “0” in front of the one digit numbers, so A is 01… then you can see that 0111 is AK (01 and 11)
Say if you had TA, with (T = 20) and (A = 1), then TA = 201!
But 201 can be (B = 2) and (A = 1) = BA!
Actually, TA would be 2001 and BA would be 0201.
What system are YOU using?
His system…

His system encodes A as 01, B as 02, T as 20, etc. It should then decode it the same way, so 0201 would decode as BA and 2001 would decode as TA.
Ok, I though A was just 1, not 01 for the system you were using…
dinosaur_a
Scratcher
20 posts

Converting usernames to numbers, please help

AProductions wrote:

How could I convert a username into numbers for use in a variable? In other words how could I translate letters to numbers? Please help
ThebroofScratch
Scratcher
29 posts

Converting usernames to numbers, please help

AProductions wrote:

How could I convert a username into numbers for use in a variable? In other words how could I translate letters to numbers? Please help
@AProductions I had a quick snoop and test, I found one and I modified it and put it in one of my projects. I'll put links for original and my modified one.
Just so you know, in mine ConvertUser is convert it back, ConvertUsername is to convert it to numbers. On Extreme Ninja Runner, ConvertHiScore is convert it back, and ConvertUserame is convert it to numbers. That one needs a high score thing so i recommened mine. Hope this helped!
Mine: https://scratch.mit.edu/projects/90573207/
Extreme Ninja Runner: https://scratch.mit.edu/projects/45836888/

Last edited by ThebroofScratch (Jan. 23, 2016 20:02:01)

Deathlord101
Scratcher
2 posts

Converting usernames to numbers, please help

defineencodestringsetletterstoabcdefghijklmnopqrstuvwxyz1234567890`~!@#$%^&*()-=_+[{}|;:'",./?]setindexto1repeatlengthofstringsetcindexto1repeatuntillettercindexof(letters)=letterindexofstring>changecindexby1

Last edited by Deathlord101 (Feb. 16, 2017 14:24:53)

Deathlord101
Scratcher
2 posts

Converting usernames to numbers, please help

Deathlord101 wrote:

defineencodestringsetletterstoabcdefghijklmnopqrstuvwxyz1234567890`~!@#$%^&*()-=_+[{}|;:'",./?]setindexto1repeatlengthofstringsetcindexto1repeatuntillettercindexof(letters)=letterindexofstring>changecindexby1

Last edited by Deathlord101 (Feb. 16, 2017 14:25:46)

25mgreen
Scratcher
8 posts

Converting usernames to numbers, please help

How do you convert it back to numbers and letters?
deck26
Scratcher
1000+ posts

Converting usernames to numbers, please help

25mgreen wrote:

How do you convert it back to numbers and letters?
Please try to avoid necroposting, better to create a new topic.

Decoding is simpler though. Take two digits from your input and they represent the number of a character (either in a list or using the letter n of textstring method). Use the join block to join each character to what you have so far as you decode.
Powerr_Electron
Scratcher
8 posts

Converting usernames to numbers, please help

Hey AProductions,
It is possible to make the user's username a variable, Scratch already makes it a variable by default, so you can use the sample code below for an idea.
whenspacekeypressedsayYour username is...sayusernamestopthis script
Thank you,
Benjamin Case
marcelzietek2006
Scratcher
500+ posts

Converting usernames to numbers, please help

Powerr_Electron wrote:

Hey AProductions,
It is possible to make the user's username a variable, Scratch already makes it a variable by default, so you can use the sample code below for an idea.
whenspacekeypressedsayYour username is...sayusernamestopthis script
Thank you,
Benjamin Case
Please don't necropost

Powered by DjangoBB