1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
| Option Explicit
Function Decode_UTF8(sStr As String) As String
Dim c0 As Long, c1 As Long, c2 As Long, c3 As Long
Dim n As Long
Dim sTxt As String
If isUTF8(sStr) = False Then
Decode_UTF8 = sStr
Exit Function
End If
sTxt = ""
n = 1
Do While n <= Len(sStr)
c0 = Asc(Mid$(sStr, n, 1))
If n <= Len(sStr) - 1 Then
c1 = Asc(Mid$(sStr, n + 1, 1))
Else
c1 = 0
End If
If n <= Len(sStr) - 2 Then
c2 = Asc(Mid$(sStr, n + 2, 1))
Else
c2 = 0
End If
If n <= Len(sStr) - 3 Then
c3 = Asc(Mid$(sStr, n + 3, 1))
Else
c3 = 0
End If
If (c0 And 240) = 240 And (c1 And 128) = 128 And (c2 And 128) = 128 And (c3 And 128) = 128 Then
sTxt = sTxt + ChrW((c0 - 240) * 65536 + (c1 - 128) * 4096) + (c2 - 128) * 64 + (c3 - 128)
n = n + 4
ElseIf (c0 And 224) = 224 And (c1 And 128) = 128 And (c2 And 128) = 128 Then
sTxt = sTxt + ChrW((c0 - 224) * 4096 + (c1 - 128) * 64 + (c2 - 128))
n = n + 3
ElseIf (c0 And 192) = 192 And (c1 And 128) = 128 Then
sTxt = sTxt + ChrW((c0 - 192) * 64 + (c1 - 128))
n = n + 2
ElseIf (c0 And 128) = 128 Then
sTxt = sTxt + ChrW(c0 And 127)
n = n + 1
Else ' c0 < 128
sTxt = sTxt + ChrW(c0)
n = n + 1
End If
Loop
Decode_UTF8 = sTxt
End Function
Private Function isUTF8(sStr As String) As Boolean
Dim c0 As Long, c1 As Long, c2 As Long, c3 As Long
Dim n As Integer
isUTF8 = True
n = 1
Do While n <= Len(sStr)
c0 = Asc(Mid$(sStr, n, 1))
If n <= Len(sStr) - 1 Then
c1 = Asc(Mid$(sStr, n + 1, 1))
Else
c1 = 0
End If
If n <= Len(sStr) - 2 Then
c2 = Asc(Mid$(sStr, n + 2, 1))
Else
c2 = 0
End If
If n <= Len(sStr) - 3 Then
c3 = Asc(Mid$(sStr, n + 3, 1))
Else
c3 = 0
End If
If (c0 And 240) = 240 Then
If (c1 And 128) = 128 And (c2 And 128) = 128 And (c3 And 128) = 128 Then
n = n + 4
Else
isUTF8 = False
Exit Function
End If
ElseIf (c0 And 224) = 224 Then
If (c1 And 128) = 128 And (c2 And 128) = 128 Then
n = n + 3
Else
isUTF8 = False
Exit Function
End If
ElseIf (c0 And 192) = 192 Then
If (c1 And 128) = 128 Then
n = n + 2
Else
isUTF8 = False
Exit Function
End If
ElseIf (c0 And 128) = 0 Then
n = n + 1
Else
isUTF8 = False
Exit Function
End If
Loop
End Function |
Partager