.net environment character encoding type associated operating instructions

In our program, there is often a swap operation on ASCII code, unicode, utf8 and so many character encoding. Though it possible to use the windows API function, but there dotNet environment more simple and convenient classes to get the job done.

Character is a number of different schemes character or code page represented abstract entities. For example, Unicode UTF-16 character encoding sequence is represented as 16-bit integers, and the Unicode UTF-8 character encoding the same will be represented as a sequence of 8-bit bytes. The common language runtime uses Unicode UTF-16 (Unicode Transformation Format, 16-bit encoded form) represents the character.

For applications using a common language runtime character encoding tabular form of a map from the native character scheme to other programs. Applications using the decoded characters from non-native program map to the machine program. The following table lists tabindex = "0" keywords = "frlrfSystemText"> System.Text most common class name space character encoding and decoding.

Character program class Explanation
ASCII encoding tabindex="0" keywords="frlrfSystemTextASCIIEncodingClassTopic">System.Text.ASCIIEncoding The character with the ASCII character conversion.
A variety of coding tabindex="0" keywords="frlrfSystemTextEncodingClassTopic">System.Text.Encoding The characters tabindex = "0" keywords = "frlrfSystemTextEncodingClassConvertTopic"> Convert various encoding methods specified conversion.
UTF-16 Unicode encoding tabindex="0" keywords="frlrfSystemTextUnicodeEncodingClassTopic">System.Text.UnicodeEncoding Converting between other encoding UTF-16 code. This program will be represented as a 16-bit integer character.
UTF-8 Unicode encoding tabindex="0" keywords="frlrfSystemTextUTF8EncodingClassTopic">System.Text.UTF8Encoding Converting between other encoding UTF-8 encoding. This variable-width character encoding scheme represented by one to four bytes.

The following code example uses tabindex = "0" keywords = "frlrfSystemTextASCIIEncodingClassGetBytesTopic"> ASCIIEncoding.GetBytes method to convert Unicode strings byte array. Each byte in the array represents the position in the string of ASCII values ​​letters.

[Visual Basic]
Dim MyString As String = "Encoding String."
Dim AE As New ASCIIEncoding()
Dim ByteArray As Byte() = AE.GetBytes(MyString)
Dim x as Integer
For x = 0 To ByteArray.Length - 1
   Console.Write("{0} ", ByteArray(x))
Next

[C#]
string MyString = "Encoding String.";
ASCIIEncoding AE = new ASCIIEncoding();
byte[] ByteArray = AE.GetBytes(MyString);
for(int x = 0;x <= ByteArray.Length - 1; x++)
{
   Console.Write("{0} ", ByteArray[x]);
}

This example will show the following to the console. Byte 69character EASCII value of byte 110character nASCII value, and so on.

69 110 99 111 100 105 110 103 32 83 116 114 105 110 103 46

The following code example uses ASCIIEncoding class to convert the byte array in front of an array of characters. tabindex = "0" keywords = " frlrfSystemTextASCIIEncodingClassGetCharsTopic"> GetChars method for decoding an array of bytes.

[Visual Basic]
Dim AE As New ASCIIEncoding()
Dim ByteArray As Byte() = { 69, 110, 99, 111, 100, 105, 110, 103, 32, 83, 116, 114, 105, 110, 103, 46 }
Dim CharArray As Char() = AE.GetChars(ByteArray)
Dim x As Integer
For x = 0 To CharArray.Length - 1
   Console.Write(CharArray(x))
Next

[C#]
ASCIIEncoding AE = new ASCIIEncoding();
byte[] ByteArray = { 69, 110, 99, 111, 100, 105, 110, 103, 32, 83, 116, 114, 105, 110, 103, 46 };
char[] CharArray = AE.GetChars(ByteArray);
for(int x = 0;x <= CharArray.Length - 1; x++)
{
   Console.Write(CharArray[x]);
}

The above code will Encoding String.text to the console.

发布了30 篇原创文章 · 获赞 2 · 访问量 5万+

Guess you like

Origin blog.csdn.net/khzide/article/details/502940