Text Analysis – Detect Encoding and Get String
Detects the encoding of a byte array based on its byte order mark (BOM) and converts it to a string.
- If the byte array starts with EF BB BF, UTF-8 with BOM is used.
- If the byte array starts with FF FE, UTF-16 LE is assumed.
- If the byte array starts with FE FF, UTF-16 BE is assumed.
- If no BOM is detected, UTF-8 without BOM is used as the default.
VB
Public Function DetectEncodingAndGetString(bytes As Byte()) As String
If bytes Is Nothing Then Return String.Empty
Dim enc As Encoding = Encoding.UTF8
If bytes.Length >= 3 AndAlso bytes(0) = &HEF AndAlso bytes(1) = &HBB AndAlso bytes(2) = &HBF Then
enc = New UTF8Encoding(encoderShouldEmitUTF8Identifier:=True)
ElseIf bytes.Length >= 2 AndAlso bytes(0) = &HFF AndAlso bytes(1) = &HFE Then
enc = Encoding.Unicode
ElseIf bytes.Length >= 2 AndAlso bytes(0) = &HFE AndAlso bytes(1) = &HFF Then
enc = Encoding.BigEndianUnicode
End If
Return enc.GetString(bytes)
End Function