Skip to sidebar Skip to content
Ipercube Technical
Jack of all trades, master of none
Date: Time: - Stardate:
Welcome, Log in by clicking  Here!
SS64
MDN
Stack
Code
GitHub
Wiki
Convert
V Studio
  • Home
  • VB Functions
    • Access Functions
    • Assembly Info
    • Color/Image Functions
    • Data Table Functions
    • Email Functions
    • Active Directory Functions
    • LDAP Functions
    • Excel Functions
    • Logging Functions
    • Source Code Retrieval
    • String Functions
    • Sql Functions
    • Object Utilities Functions
    • System and Utilities
    • Utility Forms
  • VB Classes
    • Environment Variables Helper
  • ABAP
    • ABAP Reports
    • ABAP HTTP Functions
Ipercube Technical
  • Home
  • VB Functions
    • Access Functions
    • Assembly Info
    • Color/Image Functions
    • Data Table Functions
    • Email Functions
    • Active Directory Functions
    • LDAP Functions
    • Excel Functions
    • Logging Functions
    • Source Code Retrieval
    • String Functions
    • Sql Functions
    • Object Utilities Functions
    • System and Utilities
    • Utility Forms
  • VB Classes
    • Environment Variables Helper
  • ABAP
    • ABAP Reports
    • ABAP HTTP Functions

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

Posted by ipercube


© 2026 Ipercube Design