Working with the Greek Alphabet and C#

How to Display ASCII Codes Correctly when Creating a C# Application

Working with the Greek Alphabet and C# - Mark Alexander Bain
Working with the Greek Alphabet and C# - Mark Alexander Bain
The Greek alphabet cannot be displayed directly as part of a C# application. Instead ASCII codes must be used. Fortunatley that is not very difficult.

When it comes to displaying Greek letters all programmers have a problem regardless of the programming language that they're using. There's no keyboard button for the letters that they want to use. For example, a C# application might want to display:

Volume of a sphere = 4 / 3 * π * r3

However, initially, they might think that this is not possible, but it is. The secret is to use ASCII codes.

ASCII Codes

ASCII (or the American Standard Code for Information Interchange) codes are simply numbers used to represent letters, numbers and symbols (as well as actions such as carriage returns and tabs). The Greek alphabet occupies two distinct ranges in this set of ASCII codes. These are:

  • 913 to 936 = upper case Greek letters
  • 945 to 968 = lower case Greek letters

It's then just a matter of getting C# to interpret these codes correctly

Interpreting ASCII Codes with C#

C#, of course, comes with a relatively simple way of converting the code (in the form of an integer) into a Greek letter:

int ASCII = 960;
MessageBox.Show (Convert.ToChar(ASCII).ToString());

If the programmer runs this code then they will see π displayed on the screen, and this simple technique can be extended to output the full alphabet to a text file.

Using the Correct C# Namespaces

As always the first step to creating a C# application is to define the namespaces that will be used. The simplest console application will at least require the System namespace:

using System;

And, in this case, a file will be written to, and so the IO namespace will be needed as

well:

using System.IO;

The programmer is now ready to create the new C# class.

Setting Up the Class Parameters

Rather than hard coding numbers throughout the application, it is much more efficient to define them at the start of the class. For example, the start of the ASCII range can be set:

class Program {
private static int ascii_start = 913;

And then the offset between the upper and lower case letters:

private static int ascii_offset = 32;

And finally the list of Greek letter names:

private static String [] greek_letters = {
"Alpha","Beta","Gamma","Delta", "Epsilon", "Zeta",
"Eta", "Theta", "Iota", "Kappa", "Lambda", "Mu",
"Nu", "Xi", "Omicron", "Pi", "Rho", "Sigma",
"Tau", "Upsilon", "Phi", "Chi", "Psi", "Omega"
};

These can all be used in the application in order to display the letters correctly.

Creating an Output File

The aim of this command line application is to write the complete Greek alphabet to a file, and so the first step is to define the file name:

public static void Main(string[] args) {
String oFileName = "c:\\csharp\\greek.txt";

And then to ensure that the file does not already exist:

Console.WriteLine ("Creating " + oFileName);
if (File.Exists(oFileName)) {
File.Delete(oFileName);
}

Next, the code creates the file:

FileStream oFile = new FileStream (oFileName, FileMode.CreateNew);

And then it opens the file so that it is ready to be written to:

StreamWriter oWriter = new StreamWriter (oFile);

The file is now ready to receive the alphabet.

Writing the Greek Alphabet to a File with C#

The class information about the Greek alphabet are used in a loop:

Console.WriteLine ("Storing Greek letters");
for (int I = ascii_start; I < ascii_start + greek_letters.Length; i++) {

The code then uses the loop index to define the ASCII codes and the results can be written to the file:

oWriter.WriteLine (
Convert.ToChar(i).ToString() + " "
+ Convert.ToChar(i + ascii_offset).ToString() + " "
+ greek_letters [ i - ascii_start]
);
}

And finally the file must be closed:

oWriter.Close();
oFile.Close();
Console.WriteLine ( oFileName + " closed");
Console.Write("Press any key to continue . . . ");
Console.ReadKey(true);
}
}

If the application is compiled and run at this point (as shown in figure 1 at the bottom of this article) then a new file will be created. If the application user examines this file (as shown in figure 2) then they will find that it contains the complete Greek alphabet, both in upper and lower case, and with the correct letter name.

Mark Alexander Bain - Mark Alexander Bain is a writer, Mo Bro and consultant for all aspects of software development at dsquared. He has also written regularly ...

rss
Advertisement
Advertisement
Advertisement