asp.net.ph

Skip Navigation Links

Encoding Base Types

Getting Started   ASP.NET Base Types


Characters are abstract entities that can be represented using many different character schemes or code pages.

For example, Unicode UTF-16 encoding represents characters as sequences of 16-bit integers, while Unicode UTF-8 represents the same characters as sequences of 8-bit bytes. The common language runtime uses Unicode UTF-16 ( Unicode Transformation Format, 16-bit encoding form ) to represent characters.

Applications that target the common language runtime use encoding to map character representations from the native character scheme to other schemes. Applications use decoding to map characters from non-native schemes to the native scheme. The following table lists the most commonly used classes in the System.Text namespace to encode and decode characters.

Character Scheme Class Explanation
ASCII encoding System.Text.ASCIIEncoding Converts to and from ASCII characters.
Multiple encoding System.Text.Encoding Converts characters to and from various encodings as specified in the Convert method.
UTF-16 Unicode encoding System.Text.UnicodeEncoding Converts to and from UTF-16 encoding. This scheme represents characters as 16-bit integers.
UTF-8 Unicode encoding System.Text.UTF8Encoding Converts to and from UTF-8 encoding. This variable-width encoding scheme represents characters using one to four bytes.

The following code example converts a Unicode string into an array of bytes using the ASCIIEncoding.GetBytes method. Each byte in the array represents the ASCII value for the letter in that position of the string.

string myString = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
ASCIIEncoding AE = new ASCIIEncoding ( );
byte [ ] alphabetArray = AE.GetBytes ( myString );

for ( int x = 0; x <= alphabetArray.Length - 1; x++ ) {
   Response.Write ( string.Format ( "{0}", alphabetArray [ x ] + ", " );
}
  C# VB

which displays the following on the page. The byte 65 is the ASCII value for the A character; the byte 66 is the ASCII value for the B character, and so on.

65, 66, 67, 68, 69, 70, ... ... 86, 87, 88, 89, 90

 Show me 

The following code example converts the preceding array of bytes into an array of characters using the ASCIIEncoding class. The GetChars method is used to decode the array of bytes.

ASCIIEncoding AE = new ASCIIEncoding ( );
byte [ ] alphabetArray = { 69, 110, 99, 111, 100, 105, 110, 103, 32, 83, 116, 114, 105, 110, 103, 46 };
char [ ] CharArray = AE.GetChars ( alphabetArray );

for ( int x = 0;x <= CharArray.Length - 1; x++ ) {
   Response.Write ( CharArray [ x ] );
}
  C# VB

which displays the characters ABCDEFGHIJKLMNOPQRSTUVWXYZ on the page.

 Show me 

See Also

System.Text



© 2025 Reynald Nuñez and asp.net.ph. All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note