• Home
  • Blog
  • Demos
  • About
  • ------------------
  • Articles
  • Csharp(59)
    • controls (4)
    • methods (5)
    • tutorials (50)
  • Html(1)
    • tutorials (1)
  • Java(10)
    • mobile (10)
  • Javascript(5)
    • tutorials (5)
  • Linux(3)
    • tutorial (3)
  • Math(10)
    • tutorials (10)
  • Php(15)
    • functions (1)
    • mysql (6)
    • tutorials (8)
  • Sql(23)
    • tutorials (23)
  • Vba(1)
    • tutorials (1)

Creating an extended ASCII table in C#

Written: 2010-02-05 06:55:54
Mood: Stupid
Subject: programming
Right! Today I had an urge to create something, so I decided that I'd try to make an ASCII-table. You know, these tables that you once in a while google for in order to find the HTML entity name for < or >.

This seemed like an interesting idea now since I'm also in the process of learning about binaries. This way, I could list all ASCII characters in binary as well.
The language of choice? C#...

There were some problems to tackle first though.
C# doesn't provide a way to convert numbers or characters into what I'd call "eye-candy binaries". That is, if you convert the number 2 to binaries, C# outputs 10, not 0010. So I had to come up with a way to fix this.

Another problem was that wanted to list all HTML entities for somewhat unusual and dangerous characters (such as tags, amps etc.) and I couldn't find a way to do this.

Well, it all worked out for the best, the list can be found here. Since I was so happy with it, I just had to put it here on MorkaLork.
The C# app looked something like this:

imagehttp://admin.morkalork.com/uploads/images/csharp/ASCII_table1.png

Now, let me show you how to create a list like this:


Problem 1: Proper binaries



Since this exteneded ASCII-table holds 255 chars, it would be logical to display every character in 8-bits. As I stated earlier, problem with the Convert class is that the ToString() method can convert a number from base 10 (decimal) to base 2 (binary), but it only returns the number of bits needed to create the binary.
So, what I did was that I subtracted the length of the returned value with 8 and added zero´s according the the left over.

This is how the code looks:

//Get the binary string by converting the string to base two
string binary = Convert.ToString(i, 2);

/* Since we're dealing with an 8-bit value(255) * we want to display the binary version with 8 * digits. This little loop adds zeroes to any * value that contains less than 8 zeroes */
if(binary.Length < 8)
{
int shortComming = 8 - binary.Length;
for (int j = 0;j < shortComming ;j++ )
binary = "0" + binary;
}


As we can see, if the length of the binary is less than 8, we loop from 0 to the number of bits missing, adding a string zero every time.

Problem 2: The HTML-entities



This was a bit trickier. There was the easy way, String.Format("&#{0};", i}; where you just get the actual number entity, but I wanted to get the names, and only the ones that are important.

I assumed that the .NET library had some way of helping me out here, and I was right. After massive googling on the subject I found that the System.Web namespace holds a class called HttpUtility which holds a method called HtmlEncode() that encodes a string to the corresponding HTML entities. This was perfect.

This is what the code looks like:

//Get the HTML entity string
string webEntity = System.Web.HttpUtility.HtmlEncode(c.ToString());
//We don't want the regular chars:
if(webEntity == c.ToString())
webEntity = "";


The rest worked as a charm. The source code for this project can be downloaded here:
ASCII-table.rar

Phew, it worked out...