Private Function Utf8ToUni(B() As Byte) As String '不使用 API,将 Utf8 转换为 Unicode Dim BU As Long On Error Resume Next BU = -1: BU = UBound(B) If BU = -1 Then Exit Function On Error GoTo 0 Dim I As Long,K As Long,N As Long Dim B1 As Byte,cnt As Byte I = LBound(B) If BU > I + 1 Then If B(I) = 239 And B(I + 1) = 187 And B(I + 2) = 191 Then I = I + 3 '去掉前三个字符 End If Do If I > BU Then Exit Do B1 = B(I) If (B1 And &HFC) = &HFC Then cnt = 6 ElseIf (B1 And &HF8) = &HF8 Then cnt = 5 ElseIf (B1 And &HF0) = &HF0 Then cnt = 4 ElseIf (B1 And &HE0) = &HE0 Then cnt = 3 ElseIf (B1 And &HC0) = &HC0 Then cnt = 2 Else cnt = 1 End If If I + cnt - 1 > BU Then Utf8ToUni = Utf8ToUni & "?": Exit Do Select Case cnt Case 2: N = B1 And &H1F Case 3: N = B1 And &HF Case 4: N = B1 And &H7 Case 5: N = B1 And &H3 Case 6: N = B1 And &H1 Case Else: Utf8ToUni = Utf8ToUni & Chr(B1): GoTo Next1 End Select For K = 1 To cnt - 1 N = N * &H40 + (B(I + K) And &H3F) Next Utf8ToUni = Utf8ToUni & ChrW(N)Next1: I = I + cnt LoopEnd Function
原文链接:https://www.f2er.com/vb/262001.html