作者:VB.NET开发
项目:System.Tex
' 导入命名空间
Imports System.Text
Class ASCIIEncodingExample
Public Shared Sub Main()
' The encoding.
Dim ascii As New ASCIIEncoding()
' A Unicode string with two characters outside the ASCII code range.
Dim unicodeString As String = _
"This Unicode string contains two characters " & _
"with codes outside the ASCII code range, " & _
"Pi (" & ChrW(928) & ") and Sigma (" & ChrW(931) & ")."
Console.WriteLine("Original string:")
Console.WriteLine(unicodeString)
' Save positions of the special characters for later reference.
Dim indexOfPi As Integer = unicodeString.IndexOf(ChrW(928))
Dim indexOfSigma As Integer = unicodeString.IndexOf(ChrW(931))
' Encode string.
Dim encodedBytes As Byte() = ascii.GetBytes(unicodeString)
Console.WriteLine()
Console.WriteLine("Encoded bytes:")
Dim b As Byte
For Each b In encodedBytes
Console.Write("[{0}]", b)
Next b
Console.WriteLine()
' Notice that the special characters have been replaced with
' the value 63, which is the ASCII character code for '?'.
Console.WriteLine()
Console.WriteLine( _
"Value at position of Pi character: {0}", _
encodedBytes(indexOfPi) _
)
Console.WriteLine( _
"Value at position of Sigma character: {0}", _
encodedBytes(indexOfSigma) _
)
' Decode bytes back to string.
' Notice missing Pi and Sigma characters.
Dim decodedString As String = ascii.GetString(encodedBytes)
Console.WriteLine()
Console.WriteLine("Decoded bytes:")
Console.WriteLine(decodedString)
End Sub
End Class