Skip to content

Instantly share code, notes, and snippets.

@ShaneGowland
Created May 9, 2013 09:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ShaneGowland/5546565 to your computer and use it in GitHub Desktop.
Save ShaneGowland/5546565 to your computer and use it in GitHub Desktop.
Check if a file is unicode encoded
Private Function is_unicode(ByVal path As String) As Boolean
Dim enc As System.Text.Encoding = Nothing
Dim file As System.IO.FileStream = New System.IO.FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read)
If file.CanSeek Then
Dim bom As Byte() = New Byte(3) {} ' Get the byte-order mark, if there is one
file.Read(bom, 0, 4)
If (bom(0) = &HEF AndAlso bom(1) = &HBB AndAlso bom(2) = &HBF) OrElse (bom(0) = &HFF AndAlso bom(1) = &HFE) OrElse (bom(0) = &HFE AndAlso bom(1) = &HFF) OrElse (bom(0) = 0 AndAlso bom(1) = 0 AndAlso bom(2) = &HFE AndAlso bom(3) = &HFF) Then ' ucs-4
Return True
Else
Return False
End If
' Now reposition the file cursor back to the start of the file
file.Seek(0, System.IO.SeekOrigin.Begin)
Else
Return False
End If
End Function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment