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. : UTF8Encoding « Development « VB.Net Tutorial

Home
VB.Net Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statements
5.Date Time
6.Class Module
7.Development
8.Collections
9.Generics
10.Attributes
11.Event
12.LINQ
13.Stream File
14.GUI
15.GUI Applications
16.Windows Presentation Foundation
17.2D Graphics
18.I18N Internationlization
19.Reflection
20.Regular Expressions
21.Security
22.Socket Network
23.Thread
24.Windows
25.XML
26.Database ADO.net
27.Design Patterns
VB.Net
VB.Net by API
VB.Net Tutorial » Development » UTF8Encoding 
7.32.4.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 - 1As Byte
      myEnc.GetBytes(myChars, 0, myChars.Length, myBytes, 0, True)

      Dim As Integer
      For i = 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 - 1As Char
      myDec.GetChars(myBytes, 0, myBytes.Length, myDecodedChars, 0, True)

      Console.WriteLine(myDecodedChars)

   End Sub
End Class
7.32.UTF8Encoding
7.32.1.Create a new UTF8Encoding
7.32.2.GetChars method decodes a range of elements in a byte array and store the result in a character array.
7.32.3.GetCharCount returns the number of characters produced by decoding a range of elements in a byte array using UnicodeEncoding.
7.32.4.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.
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.