Convert string to and back: UTF8, UTF7, Unicode and UTF32 : UTF8 UTF7 UTF16 « 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 » UTF8 UTF7 UTF16 
7.31.3.Convert string to and back: UTF8, UTF7, Unicode and UTF32
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 <> quoteThen _
           Throw New Exception("UTF7 Conversion Error")
        If (fromUTF8 <> quoteThen _
           Throw New Exception("UTF8 Conversion Error")
        If (fromUnicode <> quoteThen _
           Throw New Exception("Unicode Conversion Error")
        If (fromUTF32 <> quoteThen _
           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
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.