Si vous êtes utilisateur de web services ou si vous avez besoin d’encoder des chaînes de caractères en UTF-8,
Cette fonction en VBA va vous aider…
Public Function URIEncodeUTF8(ByVal sURI As String) As String
'Author : Philben - v1.0 - free to use
Dim sUTF8 As String, lCode As Long, i As Long, abyURI() As Byte
abyURI = StrConv(sURI, vbUnicode)
For i = 0 To UBound(abyURI) Step 4
lCode = abyURI(i) + abyURI(i + 1) * &H10& + abyURI(i + 2) * &H100& + abyURI(i + 3) * &H1000&
Select Case lCode
Case 48 To 57, 65 To 90, 97 To 122 '1-9, A-Z, a-z => pas d'encodage
sUTF8 = sUTF8 & Chr(lCode)
Case Is < &H80& 'de 0 Ã 127
sUTF8 = sUTF8 & Format(Hex(lCode), "%@@")
Case Is < &H800& 'de 128 Ã 2047
sUTF8 = sUTF8 & Format(Hex(&HC0& Or (lCode \ &H40&)) & _
Hex(&H80& Or (lCode And &H3F&)), "%@@%@@")
Case Is < &H10000 'de 2048 Ã 65535
sUTF8 = sUTF8 & Format(Hex(&HE0& Or (lCode \ &H1000&)) & _
Hex(&H80& Or ((lCode \ &H40&) And &H3F&)) & _
Hex(&H80& Or (lCode And &H3F&)), "%@@%@@%@@")
Case Else '>= 65536
sUTF8 = sUTF8 & Format(Hex(&HF0& Or (lCode \ &H40000)) & _
Hex(&H80& Or ((lCode \ &H1000&) And &H3F&)) & _
Hex(&H80& Or ((lCode \ &H40&) And &H3F&)) & _
Hex(&H80& Or (lCode And &H3F&)), "%@@%@@%@@%@@")
End Select
Next i
URIEncodeUTF8 = sUTF8
End Function
'Author : Philben - v1.0 - free to use
Dim sUTF8 As String, lCode As Long, i As Long, abyURI() As Byte
abyURI = StrConv(sURI, vbUnicode)
For i = 0 To UBound(abyURI) Step 4
lCode = abyURI(i) + abyURI(i + 1) * &H10& + abyURI(i + 2) * &H100& + abyURI(i + 3) * &H1000&
Select Case lCode
Case 48 To 57, 65 To 90, 97 To 122 '1-9, A-Z, a-z => pas d'encodage
sUTF8 = sUTF8 & Chr(lCode)
Case Is < &H80& 'de 0 Ã 127
sUTF8 = sUTF8 & Format(Hex(lCode), "%@@")
Case Is < &H800& 'de 128 Ã 2047
sUTF8 = sUTF8 & Format(Hex(&HC0& Or (lCode \ &H40&)) & _
Hex(&H80& Or (lCode And &H3F&)), "%@@%@@")
Case Is < &H10000 'de 2048 Ã 65535
sUTF8 = sUTF8 & Format(Hex(&HE0& Or (lCode \ &H1000&)) & _
Hex(&H80& Or ((lCode \ &H40&) And &H3F&)) & _
Hex(&H80& Or (lCode And &H3F&)), "%@@%@@%@@")
Case Else '>= 65536
sUTF8 = sUTF8 & Format(Hex(&HF0& Or (lCode \ &H40000)) & _
Hex(&H80& Or ((lCode \ &H1000&) And &H3F&)) & _
Hex(&H80& Or ((lCode \ &H40&) And &H3F&)) & _
Hex(&H80& Or (lCode And &H3F&)), "%@@%@@%@@%@@")
End Select
Next i
URIEncodeUTF8 = sUTF8
End Function
Exemple d’utilisation
? URIEncodeUTF8("un hélicoptère") 'Chaîne obtenue : un%20h%C3%A9licopt%C3%A8re
@+
Philippe