I use this function to detect UTF8 without BOM. Just get a max of 1000 bytes from file (if less get all bytes from file)
Code:Public Function ContainsUTF8(ByRef Source() As Byte) As Boolean Dim i As Long, lUBound As Long, lUBound2 As Long, lUBound3 As Long Dim CurByte As Byte lUBound = UBound(Source) lUBound2 = lUBound - 2 lUBound3 = lUBound - 3 If lUBound > 2 Then For i = 0 To lUBound - 1 CurByte = Source(i) If (CurByte And &HE0) = &HC0 Then If (Source(i + 1) And &HC0) = &H80 Then ContainsUTF8 = True i = i + 1 Else ContainsUTF8 = False Exit For End If ElseIf (CurByte And &HF0) = &HE0 Then ' 2 bytes If (Source(i + 1) And &HC0) = &H80 Then i = i + 1 If i < lUBound2 Then If (Source(i + 1) And &HC0) = &H80 Then ContainsUTF8 = True i = i + 1 Else ContainsUTF8 = False Exit For End If Else ContainsUTF8 = False Exit For End If Else ContainsUTF8 = False Exit For End If ElseIf (CurByte And &HF8) = &HF0 Then ' 2 bytes If (Source(i + 1) And &HC0) = &H80 Then i = i + 1 If i < lUBound2 Then If (Source(i + 1) And &HC0) = &H80 Then ContainsUTF8 = True i = i + 1 If i < lUBound3 Then If (Source(i + 1) And &HC0) = &H80 Then ContainsUTF8 = True i = i + 1 Else ContainsUTF8 = False Exit For End If Else ContainsUTF8 = False Exit For End If Else ContainsUTF8 = False Exit For End If Else ContainsUTF8 = False Exit For End If Else ContainsUTF8 = False Exit For End If End If Next i End If End Function




Reply With Quote