VBScript and the ASCII Greek Alphabet

How to Display Greek Letters on a Web Site by Using VBSript

© Mark Alexander Bain

Jun 26, 2009
VBScript and the ASCII Greek Alphabet, Mark Alexander Bain
Greek letters cannot be added directly to a web page - they need ASCII codes. And a little VBScript code makes life a lot easier.

Normally any web site developer can add informatio to a web page by typing text and by using the letters that they can see on their keyboards. However the Greek Alphabet is not like that. The web site developer will find keys for neither α nor ψ, and that means that they can't directly add something like:

Circle Area = π * (Radius squared)

Or:

sin (-θ) = -sine θ

For that they'll need to turn to ASCII Codes and, to make life a little simpler, they'll need some VBScript code.

ASCII Codes and the Greek Alphabet

ASCII (American Standard Code for Information Interchange) code is added a web page's text and is interpreted by the web browser so that:

  • θ is displayed as θ
  • α is displayed as α

It's then just a matter of writing a little VBScript that will handle the ASCII codes for the web page designer.

Defining the ASCII Codes for the Greek Alphabet

The names for the Greek letters can be stored in an array:

Dim Letters : Letters = Array ( _
"Alpha","Beta","Gamma","Delta", "Epsilon", "Zeta", _
"Eta", "Theta", "Iota", "Kappa", "Lambda", "Mu", _
"Nu", "Xi", "Omicron", "Pi", "Rho", "Sigma", _
"Tau", "Upsilon", "Phi", "Chi", "Psi", "Omega")

There are also some important points to take into account:

  • there are 24 letters (as can be seen from the array)
  • there are separate ASCII codes for the upper and lower letters
  • the upper case letters occupy a range starting at Α
  • for each letter the lower case letter has an ASCII code 32 greater than the upper case letter

And these important facts can be represented by a few variables:

Dim l_start :l_start = 913
Dim l_end : l_end = l_start + ubound(Letters)
Dim l_space: l_space = 32

With those in place the programmer can use them in a function that will enable them to display the Greek letters with ease.

A Function to Display Greek Letters

The requirements of the function are quite simple:

  • to accept the name of a greek letter (e.g alpha) as an input
  • to return the ASCII code for either the lower or upper case Greek letter (depending the case of the input)

And so the first step is to give the function a suitable name and to define the variables to be used:

Function greek_letter (name)
Dim I, L

The next step is to step through the array, comparing the input to each element in the array:

For I = lbound(Letters) to ubound(Letters)
If ucase(Letters(I)) = ucase(name) Then
L = I
End If
Next

At the end of the process the index number of the letter will have been identified, and this can be used to create the correct ASCII code:

If L <> "" Then
If name = lcase(name) Then
greek_letter = "&#" & Ascii_start + L + Ascii_space
Else
greek_letter = "&#" & Ascii_start + L
End If
Else
greek_letter = "undefined"
End If
End Function

This can be seen in action by creating an array of different possible inputs:

Dim letter_list : letter_list = Array("Pi", "PI", "pi", "fi")

And then the array elements can be used as inputs to the function:

Dim I
For I = lbound(letter_list) to ubound(letter_list)
document.write(letter_list(I) & ": " & greek_letter(letter_list(I)) & "<br>")
Next

The results can be seen in figure 1 at the bottom of this article, and they show how easily a web developer can work with Greek letters in a web page either individually or en mass as discussed in Display the Greek Alphabet with VBScript .


The copyright of the article VBScript and the ASCII Greek Alphabet in Computer Programming Languages is owned by Mark Alexander Bain. Permission to republish VBScript and the ASCII Greek Alphabet in print or online must be granted by the author in writing.


VBScript and the ASCII Greek Alphabet, Mark Alexander Bain
Figure 1: Pi in a Web Page, Mark Alexander Bain
     


Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo