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:
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:
string binary = Convert.ToString(i, 2);
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:
string webEntity = System.Web.HttpUtility.HtmlEncode(c.ToString());
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...