I had to convert a letter to a number today. Weird I know, but basically I needed the letter’s numeric position in the alphabet – so A is 1, B is 2, Z is 26 and so on.
It’s simple enough in theory, but unless you’re going to create a function to lookup and return the correct number it’s a bit messy. There’s probably a neater way of using split() and returning the array index, but I couldn’t figure it out.
Just when I was about to do a horrible:
IF n = “A” THEN n = 1
IF n = “Z” THEN n = 26
I can across the function ASC() in vbscript, which gets the ASCII value. It turns out with a little bit of tweaking you can create a simple conversion function:
ConvertToNumber = Asc(ucase(n)) – 64
It’s not something you’d use that often, but it saves a lot of time when you do need it.