Odd Encoding behavior, or me ?

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Paste this code in a new Form and create files, then read then, UTF32 and UTF7 seem odd and bigendianUnicode appears as unicode, but reads correctly, just as UTF8 does.Option Strict On
Imports System.IO
Imports System.Text
Public Class Form1
WithEvents BTNFileType As New Button With {.Location = New Point(10, 10), .Size = New Size(75, 25), .Text = "File Type"}
WithEvents BTNMakeFiles As New Button With {.Location = New Point(100, 10), .Size = New Size(75, 25), .Text = "Make Files"}
WithEvents BTNReadFiles As New Button With {.Location = New Point(190, 10), .Size = New Size(75, 25), .Text = "Read Files"}
Dim TXTResults As New TextBox With {.Location = New Point(10, 50), .Multiline = True, .Size = New Size(400, 300), .Font = New Font("Courier New", 10), _
.Anchor = CType(AnchorStyles.Top + AnchorStyles.Bottom + AnchorStyles.Left + AnchorStyles.Right, AnchorStyles)}

Private Function FileType(ByVal path As String) As Encoding
Dim file As System.IO.FileStream = New System.IO.FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read)
Dim Header(3) As Byte
file.Read(Header, 0, 4)
file.Close()
If (Header(0) = &HEF AndAlso Header(1) = &HBB AndAlso Header(2) = &HBF) Then Return Encoding.UTF8
If (Header(0) = &HFF AndAlso Header(1) = &HFE) Then Return Encoding.Unicode
If (Header(0) = &HFE AndAlso Header(1) = &HFF) Then Return Encoding.BigEndianUnicode

If (Header(0) = 0 AndAlso Header(1) = 0 AndAlso Header(2) = &HFF AndAlso Header(3) = &HFE) Then Return Encoding.Unicode
If (Header(0) = 0 AndAlso Header(1) = 0 AndAlso Header(2) = &HFE AndAlso Header(3) = &HFF) Then
Return Encoding.BigEndianUnicode
Else
Return Encoding.ASCII
End If
End Function
Private Sub BTNFileType_Click(sender As System.Object, e As System.EventArgs) Handles BTNFileType.Click
Using OFD As New OpenFileDialog
TXTResults.Clear()
If OFD.ShowDialog = Windows.Forms.DialogResult.OK Then
TXTResults.Text = "Filename : " & OFD.FileName & vbNewLine & "Encoding : " & FileType(OFD.FileName).ToString
End If
End Using
End Sub

Private Sub BTNMakeFiles_Click(sender As System.Object, e As System.EventArgs) Handles BTNMakeFiles.Click
Dim S As String = "Un crédit vous engage et doit être remboursé. Vérifiez vos capacités de remboursement avant de vous engager"
Using FBD As New FolderBrowserDialog
FBD.RootFolder = Environment.SpecialFolder.MyComputer
FBD.ShowNewFolderButton = True
FBD.Description = "Choose a folder for the text files"
If FBD.ShowDialog = Windows.Forms.DialogResult.OK Then
My.Computer.FileSystem.WriteAllText(Path.Combine(FBD.SelectedPath, "UTF7.txt"), S, False, Encoding.UTF7)
My.Computer.FileSystem.WriteAllText(Path.Combine(FBD.SelectedPath, "ASCII.txt"), S, False, Encoding.ASCII)
My.Computer.FileSystem.WriteAllText(Path.Combine(FBD.SelectedPath, "Unicode.txt"), S, False, Encoding.Unicode)
My.Computer.FileSystem.WriteAllText(Path.Combine(FBD.SelectedPath, "BigEndianUnicode.txt"), S, False, Encoding.BigEndianUnicode)
My.Computer.FileSystem.WriteAllText(Path.Combine(FBD.SelectedPath, "UTF8.txt"), S, False, Encoding.UTF8)
My.Computer.FileSystem.WriteAllText(Path.Combine(FBD.SelectedPath, "UTF32.txt"), S, False, Encoding.UTF32)
End If
End Using
End Sub

Private Sub BTNReadFiles_Click(sender As System.Object, e As System.EventArgs) Handles BTNReadFiles.Click
Using OFD As New OpenFileDialog
If OFD.ShowDialog = Windows.Forms.DialogResult.OK Then
TXTResults.Clear()
Dim encoding As System.Text.Encoding = FileType(OFD.FileName)
TXTResults.AppendText("File : " & OFD.FileName & vbNewLine & "Encoding : " & encoding.ToString & vbNewLine & vbNewLine)
TXTResults.AppendText("Contents :" & vbNewLine)
Dim reader As New StreamReader(OFD.FileName, encoding)
Dim line As String = reader.ReadLine()
While Not (line Is Nothing)
TXTResults.AppendText(line & vbNewLine)
line = reader.ReadLine()
End While
TXTResults.AppendText(vbNewLine & "Header :" & vbNewLine)
Dim file As System.IO.FileStream = New System.IO.FileStream(OFD.FileName, FileMode.Open, FileAccess.Read, FileShare.Read)
Dim Header(15) As Byte
file.Read(Header, 0, 16)
file.Close()
For Each b As Byte In Header
TXTResults.AppendText(b.ToString("X2") & " ")
Next
TXTResults.AppendText(vbNewLine)
For Each b As Byte In Header
If b > 31 Then
TXTResults.AppendText(" " & Chr(b) & " ")
Else
TXTResults.AppendText(" . ")
End If
Next
End If
End Using
End Sub

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
With Me
.Size = New Size(440, 400)
.Text = "Encoding Stuff"
End With
Me.Controls.Add(BTNFileType)
Me.Controls.Add(BTNMakeFiles)
Me.Controls.Add(BTNReadFiles)
Me.Controls.Add(TXTResults)
End Sub
End Class

View the full article
 
Back
Top