1

I am coming across more and more situations in Scratch where I have to convert a number to its ACSII character or visa versa. There is no built function in the blocks for this.

My solution is to create a list of size 26 and append letters A-Z into each sequence using a variable called alphabet = "abcdefghijklmnopqrstuvwxyz" and iterating over it with a Repeat block and appending LETTER COUNT of ALPHABET to the list.The result is a list data structure with letters A_Z between location 1 to 26.In effect creating my own ASCII table.

To do a converson say from number 26 to 'Z' I have to iterate over the list to get the correct CHAR value. It really slows down the program that is heavily dependent on the CHR() feature. Is there a better or more efficient solution?

Timothy Lawman
  • 2,194
  • 6
  • 24
  • 33
  • Is the part you're having trouble with getting the letter from a number or vice versa? – Scimonster May 20 '15 at 14:42
  • both ways as o ne function is the inverse of the other both situations often crop up when writing string manipulation programs. I make some cypher programs for example. – Timothy Lawman May 21 '15 at 18:47

2 Answers2

1

My solution is to create a list of size 26 and append letters A-Z into each sequence using a variable called alphabet = "abcdefghijklmnopqrstuvwxyz"

Stop right there. You don't even need to convert it into a list. You can just look it up directly from the string.

To get a character from an index is very easy. Just use the (letter () of []) block.

To get the index of a character is more complex. Unfortunately, Scratch doesn't have a built-in way to do that. What i would do here is define a index of [] in [] custom pseudo-reporter block:

define index of (char) in (str)
set [i v] to [1]
repeat until <<(i) = (length of (str))> or <(letter (i) of (str)) = (char)>>
    change [i v] by (1)

view online

You can then call the block as index of [a] in (alphabet) and it will set the i variable to 1.

This code doesn't have any case for if the character isn't found, but the link i provided does include that, if you need.

Community
  • 1
  • 1
Scimonster
  • 32,893
  • 9
  • 77
  • 89
  • Yes I agree with the first solution and usually use that method in Python. Your second solution using the reporter block seems to iterate N times in the worst case so doesn't actually solve the second problem. Perhaps, that is what ord() does but I suspect it uses a lookup in constant time due to the ascii character set being built in electronically in a chip set? The difference in running time on encryption programs is enormous when using scratch and Python. – Timothy Lawman May 22 '15 at 23:04
  • I agree it's unfortunate, but that's how you have to do it in Scratch. – Scimonster May 24 '15 at 19:28
  • What about binary search? – Roland Illig Oct 17 '19 at 03:54
0

You could also use Snap! which is similar to Scratch, but has more blocks. Snap! has a unicode block, that will convert a character to its ASCII or unicode value.

  • Yes, Snap has built-in functions for converting a character to ASCII value and back: `unicode of []` and `unicode () as letter`. Unicode and ASCII codes are the same for characters that have an ASCII code. – Kate Fractal Jul 27 '15 at 19:57