Encoding.GetCharCount calculates the number of characters produced by decoding
Imports System
Imports System.Text
Imports Microsoft.VisualBasic
Public Class SamplesEncoding
Public Shared Sub Main()
Dim u32LE As Encoding = Encoding.GetEncoding("utf-32")
Dim u32BE As Encoding = Encoding.GetEncoding("utf-32BE")
Dim myStr As String = "za" & ChrW(&H0306) & ChrW(&H01FD) & ChrW(&H03B2)
Dim barrBE(u32BE.GetByteCount(myStr) - 1) As Byte
u32BE.GetBytes(myStr, 0, myStr.Length, barrBE, 0)
Dim barrLE(u32LE.GetByteCount(myStr) - 1) As Byte
u32LE.GetBytes(myStr, 0, myStr.Length, barrLE, 0)
PrintCountsAndChars(barrBE, u32BE)
PrintCountsAndChars(barrLE, u32LE)
End Sub 'Main
Public Shared Sub PrintCountsAndChars(bytes() As Byte, enc As Encoding)
Console.Write("{0,-25} :", enc.ToString())
Dim iCC As Integer = enc.GetCharCount(bytes)
Console.Write(" {0,-3}", iCC)
Dim iMCC As Integer = enc.GetMaxCharCount(bytes.Length)
Console.Write(" {0,-3} :", iMCC)
Dim chars As Char() = enc.GetChars(bytes)
Console.WriteLine(chars)
End Sub
End Class
Related examples in the same category