ASCII number manipulation

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
notHalfBad
Posts: 4
Joined: Thu May 26, 2016 12:47 am

ASCII number manipulation

Post by notHalfBad » Tue May 31, 2016 11:57 am

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?

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: ASCII number manipulation

Post by Klaus » Tue May 31, 2016 12:15 pm

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

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 875
Joined: Wed Nov 22, 2006 3:42 pm

Re: ASCII number manipulation

Post by Thierry » Tue May 31, 2016 12:19 pm

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
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!

jon@armasoft.co.uk
Posts: 20
Joined: Mon Apr 18, 2016 10:31 am

Re: ASCII number manipulation

Post by jon@armasoft.co.uk » Tue May 31, 2016 12:22 pm

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.

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 875
Joined: Wed Nov 22, 2006 3:42 pm

Re: ASCII number manipulation

Post by Thierry » Tue May 31, 2016 12:25 pm

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 ...
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: ASCII number manipulation

Post by Klaus » Tue May 31, 2016 12:47 pm

A = ASCII 65
a = ASCII 97
8)

notHalfBad
Posts: 4
Joined: Thu May 26, 2016 12:47 am

Re: ASCII number manipulation

Post by notHalfBad » Wed Jun 01, 2016 3:12 am

You guys are all awesome! I've solved my problem and it works really well now! :D Time to start adding features...

Post Reply