Click here to Skip to main content
15,884,425 members
Articles / Programming Languages / C#

A viewer of the Unicode character set

Rate me:
Please Sign up or sign in to vote.
3.31/5 (8 votes)
2 Jan 2011CPOL3 min read 44.2K   514   14   21
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).

C#
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.

C#
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Team Leader
Belgium Belgium
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralUnispy Pin
Daniele Fusi3-Jan-11 3:18
Daniele Fusi3-Jan-11 3:18 
GeneralIs it Open Source (Re: Unispy) Pin
Sergey Alexandrovich Kryukov7-Jan-11 6:29
mvaSergey Alexandrovich Kryukov7-Jan-11 6:29 
GeneralRe: Is it Open Source (Re: Unispy) Pin
Daniele Fusi7-Jan-11 21:18
Daniele Fusi7-Jan-11 21:18 
GeneralRe: Is it Open Source (Re: Unispy) Pin
Sergey Alexandrovich Kryukov8-Jan-11 9:10
mvaSergey Alexandrovich Kryukov8-Jan-11 9:10 
GeneralMy vote of 4 Pin
Sergey Alexandrovich Kryukov2-Jan-11 7:21
mvaSergey Alexandrovich Kryukov2-Jan-11 7:21 
GeneralMy vote of 1 Pin
Michael Chourdakis2-Jan-11 4:12
mvaMichael Chourdakis2-Jan-11 4:12 
Unneeded & waste of .net.
GeneralRe: My vote of 1 Pin
Sergey Alexandrovich Kryukov2-Jan-11 7:18
mvaSergey Alexandrovich Kryukov2-Jan-11 7:18 
GeneralRe: My vote of 1 Pin
Michael Chourdakis2-Jan-11 8:51
mvaMichael Chourdakis2-Jan-11 8:51 
GeneralRe: My vote of 1 Pin
pierre poliakoff2-Jan-11 11:05
pierre poliakoff2-Jan-11 11:05 
GeneralValid points; I agrue only against your vote of 1 (Re: My vote of 1) Pin
Sergey Alexandrovich Kryukov4-Jan-11 8:20
mvaSergey Alexandrovich Kryukov4-Jan-11 8:20 
GeneralUnicode is NOT a 16-bit code Pin
Sergey Alexandrovich Kryukov1-Jan-11 18:10
mvaSergey Alexandrovich Kryukov1-Jan-11 18:10 
GeneralRe: Unicode is NOT a 16-bit code Pin
pierre poliakoff1-Jan-11 22:48
pierre poliakoff1-Jan-11 22:48 
GeneralRe: Unicode is NOT a 16-bit code Pin
pierre poliakoff12-Jun-11 23:56
pierre poliakoff12-Jun-11 23:56 
Questioncharmap.exe? Pin
Axel Rietschin1-Jan-11 11:38
professionalAxel Rietschin1-Jan-11 11:38 
AnswerRe: charmap.exe? Pin
pierre poliakoff1-Jan-11 22:22
pierre poliakoff1-Jan-11 22:22 
GeneralRe: charmap.exe? Pin
malangon5-Jan-11 2:58
malangon5-Jan-11 2:58 
GeneralHow come you did not see Unicode?! (Re: charmap.exe?) Pin
Sergey Alexandrovich Kryukov7-Jan-11 8:40
mvaSergey Alexandrovich Kryukov7-Jan-11 8:40 
GeneralRe: How come you did not see Unicode?! (Re: charmap.exe?) Pin
malangon10-Jan-11 3:05
malangon10-Jan-11 3:05 
GeneralRe: How come you did not see Unicode?! (Re: charmap.exe?) Pin
Sergey Alexandrovich Kryukov10-Jan-11 5:36
mvaSergey Alexandrovich Kryukov10-Jan-11 5:36 
GeneralRe: How come you did not see Unicode?! (Re: charmap.exe?) Pin
malangon10-Jan-11 6:15
malangon10-Jan-11 6:15 
GeneralRe: How come you did not see Unicode?! (Re: charmap.exe?) Pin
Sergey Alexandrovich Kryukov10-Jan-11 13:37
mvaSergey Alexandrovich Kryukov10-Jan-11 13:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.