Page 1 of 1

ASCII number manipulation

Posted: Tue May 31, 2016 11:57 am
by notHalfBad
Hi, this is my first post on the LiveCode forums!

So I have a teensy problem. I have a keyDown operator which detects any key with the parameters [a-zA-Z].
Perhaps if I show you my code it will make more sense.

Code: Select all

on keyDown theKey
   if matchText(theKey,"[a-zA-Z]") then
      put (numToChar(lower(theKey))-140) into temp1
      put temp1 into keyPressed
      checkForCharacterMatch theKey
   end if
end keyDown
If you can see what I'm trying to do now, what I want is to input a single char A-Z in upper or lowercase, then:
- convert it to lowercase, then
- convert the lowercase char to ASCII then
- minus value x from that, where x is the number that would reduce the result number to 1 if I input 'a'

So yeah, basically I want it to convert the keypress of a into 1 and z into 26, including every letter in between of course.
Ignore checkForCharacterMatch, thats an external function that has no effect on my question.
The error message I get when I try to run the above code points to the first put operator under the if statement.

Could someone who's awesome help me out on this?

Re: ASCII number manipulation

Posted: Tue May 31, 2016 12:15 pm
by Klaus
Hi notHalfBad,

1. welcome to the forum! :D

2. Quick check: the name of the built-in LC function is TOlower()!
...
## put (numToChar(lower(theKey))-140) into temp1
put (numToChar(TOLOWER(theKey))-140) into temp1
...

Best

Klaus

Re: ASCII number manipulation

Posted: Tue May 31, 2016 12:19 pm
by Thierry
notHalfBad wrote:

Code: Select all

on keyDown theKey
   if matchText(theKey,"[a-zA-Z]") then
      put (numToChar(lower(theKey))-140) into temp1
      put temp1 into keyPressed
      checkForCharacterMatch theKey
   end if
end keyDown
Hi HalfGood :)

This one should work (not tested):

Code: Select all

   if matchText(theKey,"[a-zA-Z]") then
      put chartonum(  lower(theKey)) - 96 into keyPressed
      .....
HTH,

Thierry

Re: ASCII number manipulation

Posted: Tue May 31, 2016 12:22 pm
by jon@armasoft.co.uk
Thierry - wrong way round, You want charToNum!

That's odd. When I looked at this post it was from 'Thierry' and had no replies! As an aside "a-140" gives me a result of -43.

Re: ASCII number manipulation

Posted: Tue May 31, 2016 12:25 pm
by Thierry
jon@armasoft.co.uk wrote:Thierry - wrong way round, You want charToNum!

I don't get it :roll:

It looks like it is what I did write ...

Re: ASCII number manipulation

Posted: Tue May 31, 2016 12:47 pm
by Klaus
A = ASCII 65
a = ASCII 97
8)

Re: ASCII number manipulation

Posted: Wed Jun 01, 2016 3:12 am
by notHalfBad
You guys are all awesome! I've solved my problem and it works really well now! :D Time to start adding features...