Encoding.GetByteCount (Char[]) calculates the number of bytes
Imports System
Imports System.Text
Imports Microsoft.VisualBasic
Public Class SamplesEncoding
Public Shared Sub Main()
Dim myChars() As Char = {"z"c, "a"c, ChrW(&H0306), ChrW(&H01FD), ChrW(&H03B2), ChrW(&HD8FF), ChrW(&HDCFF)}
Dim u7 As Encoding = Encoding.UTF7
PrintCountsAndBytes(myChars, u7)
End Sub 'Main
Public Shared Sub PrintCountsAndBytes(chars() As Char, enc As Encoding)
Console.Write("{0,-30} :", enc.ToString())
Dim iBC As Integer = enc.GetByteCount(chars)
Console.Write(" {0,-3}", iBC)
Dim iMBC As Integer = enc.GetMaxByteCount(chars.Length)
Console.Write(" {0,-3} :", iMBC)
Dim bytes As Byte() = enc.GetBytes(chars)
PrintHexBytes(bytes)
End Sub
Public Shared Sub PrintHexBytes(bytes() As Byte)
If bytes Is Nothing OrElse bytes.Length = 0 Then
Console.WriteLine("<none>")
Else
Dim i As Integer
For i = 0 To bytes.Length - 1
Console.Write("{0:X2} ", bytes(i))
Next i
Console.WriteLine()
End If
End Sub
End Class
Related examples in the same category