vb.net - Encode string to UTF-8

前端 未结 2 453
栀梦
栀梦 2021-01-14 21:01

I\'ve made a class to encode a string

Public Class UTF8
    Public Shared Function encode(ByVal str As String)
        Dim utf8Encoding As New System.Text.UT         


        
2条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-14 21:41

    We can check if a string is UTF-8 by examining the string BOM value. This is the correct code sample:

    Public Shared Function encode(ByVal str As String) As String
        'supply True as the construction parameter to indicate
        'that you wanted the class to emit BOM (Byte Order Mark)
        'NOTE: this BOM value is the indicator of a UTF-8 string
        Dim utf8Encoding As New System.Text.UTF8Encoding(True)
        Dim encodedString() As Byte
    
        encodedString = utf8Encoding.GetBytes(str)
    
        Return utf8Encoding.GetString(encodedString)
    End Function
    

提交回复
热议问题