Mega Code Archive

 
Categories / VB.Net Tutorial / Development
 

Use an encoder and a decoder to encode a string into an array of bytes, and then decode the bytes into an array of characters.

Imports System Imports System.Text Imports Microsoft.VisualBasic Public Class MainClass    Public Shared Sub Main()       Dim u16 As New UnicodeEncoding(False, True, True)       Dim myEnc As Encoder = u16.GetEncoder()       Dim myDec As Decoder = u16.GetDecoder()       Dim myChars() As Char = {"z"c, "a"c, ChrW(&H0306)}              Dim iBC As Integer = myEnc.GetByteCount(myChars, 0, myChars.Length, True)       Dim myBytes(iBC - 1) As Byte       myEnc.GetBytes(myChars, 0, myChars.Length, myBytes, 0, True)       Dim i As Integer       For i = 0 To myBytes.Length - 1          Console.Write("{0:X2} ", myBytes(i))       Next i       Dim iCC As Integer = myDec.GetCharCount(myBytes, 0, myBytes.Length, True)       Dim myDecodedChars(iCC - 1) As Char       myDec.GetChars(myBytes, 0, myBytes.Length, myDecodedChars, 0, True)       Console.WriteLine(myDecodedChars)    End Sub End Class