They say you learn something new every day.

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:

FUNCTION ConvertToNumber(n)

   ConvertToNumber = Asc(ucase(n)) – 64

END FUNCTION 

It’s not something you’d use that often, but it saves a lot of time when you do need it. 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Tag Cloud

%d bloggers like this: