Convert string to and back: UTF8, UTF7, Unicode and UTF32 : UTF8 UTF7 UTF16 « Development « VB.Net Tutorial






Public Class Tester
    Public Shared Sub Main
        Dim quote As String = "qqqqqq"
        Dim result As New System.Text.StringBuilder

        Dim bytesUTF7 As Byte() = System.Text.Encoding.UTF7.GetBytes(quote)
        Dim bytesUTF8 As Byte() = System.Text.Encoding.UTF8.GetBytes(quote)
        Dim bytesUnicode As Byte() = System.Text.Encoding.Unicode.GetBytes(quote)
        Dim bytesUTF32 As Byte() = System.Text.Encoding.UTF32.GetBytes(quote)

        result.Append("bytesUTF7.Length = ")
        result.AppendLine(bytesUTF7.Length.ToString())
        result.Append("bytesUTF8.Length = ")
        result.AppendLine(bytesUTF8.Length.ToString())
        result.Append("bytesUnicode.Length = ")
        result.AppendLine(bytesUnicode.Length.ToString())
        result.Append("bytesUTF32.Length = ")
        result.AppendLine(bytesUTF32.Length.ToString())

        Dim fromUTF7 As String = System.Text.Encoding.UTF7.GetString(bytesUTF7)
        Dim fromUTF8 As String = System.Text.Encoding.UTF8.GetString(bytesUTF8)
        Dim fromUnicode As String = System.Text.Encoding.Unicode.GetString(bytesUnicode)
        Dim fromUTF32 As String = System.Text.Encoding.UTF32.GetString(bytesUTF32)

        If (fromUTF7 <> quote) Then _
           Throw New Exception("UTF7 Conversion Error")
        If (fromUTF8 <> quote) Then _
           Throw New Exception("UTF8 Conversion Error")
        If (fromUnicode <> quote) Then _
           Throw New Exception("Unicode Conversion Error")
        If (fromUTF32 <> quote) Then _
           Throw New Exception("UTF32 Conversion Error")

        Console.WriteLine(result.ToString())    
    End Sub

End Class
bytesUTF7.Length = 6
bytesUTF8.Length = 6
bytesUnicode.Length = 12
bytesUTF32.Length = 24








7.31.UTF8 UTF7 UTF16
7.31.1.Use different Encoding to create StreamReader: Default, Unicode, UTF8, UTF7
7.31.2.Use different Encoding to create StreamWriter: Default, Unicode, UTF8, UTF7
7.31.3.Convert string to and back: UTF8, UTF7, Unicode and UTF32