Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

A viewer of the Unicode character set

0.00/5 (No votes)
2 Jan 2011 1  
Everybody knows the ASCII table, but what about the Unicode table? View it at last with this small program.

UnicodeViewer.jpg

Introduction

Since the 80's we were familiar with the ASCII code, and know that the symbol for letter A is associated with ASCII code 65, the symbol for letter B is associated with ASCII code 66, and so on till 127 (contrarily to what is sometimes written, the ASCII code stops at 127 and not at 255). But the strings in .NET are not coded in ASCII, but in 16 bits Unicode. This means that you have access (in theory) to 65536 different symbols. This program shows how to display any Unicode character and the associated value.

Using the code

Just specify the range of codes you want to see and the number of displayed characters per line, and press the "Generate" button.

  • From 32 till 127, display usual ASCII
  • From 12352 till 12447, display Japanese Hiragana
  • ...

Points of interest

Get the symbol associated with a Unicode number

To print the symbol associated with a Unicode code, we need a way to build a one-character-long string that will contain the symbol we want (in fact, we need a modern equivalent of the good old BASIC function CHR$). To help us do that, .NET Framework provides the method UnicodeEncoding.Unicode.Getstring that converts numbers into a string of Unicode characters. The parameter to provide to UnicodeEncoding.Unicode.Getstring is a vector of bytes. If we want to make a string with only one character, we need to make a 2 bytes vector (because a Unicode code is 16 bits and we store 8 bits in each byte).

byte[] tmp=new byte[2];
StringBuilder textToAdd = new StringBuilder();
for (UInt32 i = start; i <= stop; i += step)
{
    for(UInt32 j=0;j<step;j++)
    {
        tmp[0]=(byte) (i+j);
        tmp[1]=(byte)((i+j)>>8);
        textToAdd.Append(UnicodeEncoding.Unicode.GetString(tmp));
    }   
    textToAdd.Append( Environment.NewLine);
}

Using StringBuilder

The text generated by this program can be very long (especially if you ask to generate the full range of characters from 0 to 65535), and since we generate the text symbol by symbol, the number of string modifications is huge. This means that for such an application, using a normal "string" variable is impossible (the execution time would become much too long). That's why the use of a "StringBuilder" variable is mandatory (see code example above).

Make a text field to enter numbers

For this program, I needed a text input field that accepts only numbers. I tried the MaskedTextBox, but it was not working the way I wanted, so I preferred to use a standard TextBox and use the TextChanged event to check if the text was indeed a number.

private void textBoxNumeric_TextChanged(object sender, EventArgs e)
{
    UInt32 result;
    string newText;
    TextBox textBox = (TextBox)sender;
    if (UInt32.TryParse(textBox.Text, out result) || textBox.Text.Length==0)
    {
        newText = textBox.Text;
        textBox.Tag = newText;
    }
    else
    { //not a valid number restore old value
        int caretPos=textBox.SelectionStart; 
        textBox.Text = (string)textBox.Tag;
        textBox.SelectionStart = caretPos - 1;
    }
}

If the text is a correct number, then we store it to be able to restore this value in case of invalid input. The storage of the valid value is done in the Tag attribute of the TextBox instance. If the text is invalid (i.e., it is not a number), then we put back the old value that had been stored in the Tag attribute and we put back the caret (i.e., the flashing cursor in the text box) at the right place.

Conclusion

We have here a small tool that allows to see parts of (or even the full) Unicode table. Since it is displayed in a normal text box, you can copy those characters and paste them anywhere. You can even paste them inside the source code you edit with Visual Studio! This can be useful if you want your program to display strings that contain characters not available on your keyboard.

History

  • 12-31-2010: First version.
  • 02-01-2011: Corrected source code example format.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here