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 <> 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
Use different Encoding to create StreamReader: Default, Unicode, UTF8, UTF7
Imports System.Text
Imports System.IO
Public Class Tester
Public Shared Sub Main
Dim myFileStream As FileStream
Dim myStreamReader As StreamReader
Dim strRead As String = ""
Dim StreamEncoding As Encoding
Try
StreamEncoding = Encoding.Default
"StreamEncoding = Encoding.Unicode
"StreamEncoding = Encoding.UTF8
"StreamEncoding = Encoding.UTF7
myFileStream = New FileStream("test.txt", FileMode.Open, FileAccess.Read)
myStreamReader = New StreamReader(myFileStream, StreamEncoding)
Console.WriteLine(myStreamReader.ReadToEnd)
myFileStream.Close()
myStreamReader.Close()
Catch EX As IOException
Console.WriteLine(EX.Message)
End Try
End Sub
End Class
Use different Encoding to create StreamWriter: Default, Unicode, UTF8, UTF7
Imports System.Text
Imports System.IO
Public Class Tester
Public Shared Sub Main
Dim myFileStream As FileStream
Dim myStreamWriter As StreamWriter
Dim strWrite As String
Dim StreamEncoding As Encoding
Try
StreamEncoding = Encoding.Default
"StreamEncoding = Encoding.Unicode
"StreamEncoding = Encoding.UTF8
"StreamEncoding = Encoding.UTF7
myFileStream = New FileStream ("test.txt", FileMode.OpenOrCreate, FileAccess.Write)
myStreamWriter = New StreamWriter(myFileStream, StreamEncoding)
strWrite = "asdf"
myStreamWriter.Write(strWrite)
myStreamWriter.Close()
myFileStream.Close()
Catch EX As IOException
Console.WriteLine(EX.Message)
End Try
End Sub
End Class