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;
StringBuilder textToAdd = new StringBuilder();
for (UInt32 i = start; i <= stop; i += step)
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)
TextBox textBox = (TextBox)sender;
if (UInt32.TryParse(textBox.Text, out result) || textBox.Text.Length==0)
newText = textBox.Text;
textBox.Tag = newText;
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.
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.
- 12-31-2010: First version.
- 02-01-2011: Corrected source code example format.