- Код: Выделить всё
Public Function EncodeUTF8(ByVal strSrc As String) As String
Dim nLen As Long
Dim strDst As String
Dim strRet As String
Dim nRet As Long
Dim p As Long
If Len(strSrc) = 0 Then Exit Function
nLen = Len(strSrc) * 2
strDst = String(nLen, vbNullChar)
strRet = String(nLen, vbNullChar)
p = StrPtr(strDst)
nRet = WideCharToMultiByte(65001, 0, StrPtr(strSrc), nLen, p, nLen, ByVal 0, 0)
nRet = MultiByteToWideChar(0, &H1, p, nLen, StrPtr(strRet), nLen)
nLen = InStr(strRet, vbNullChar)
If nLen = 0 Then
EncodeUTF8 = strRet
Else
EncodeUTF8 = Mid$(strRet, 1, nLen - 1)
End If
End Function
- Код: Выделить всё
Public Function DecodeUTF8(ByVal sInput As String) As String
Dim iStrSize As Long, lMaxSize As Long, str1 As String
Dim p As Long
Dim str2 As String
If Len(sInput) = 0 Then Exit Function
Debug.Assert Not sInput Like "*[à-ÿ]*"
lMaxSize = Len(sInput) * 2
str1 = String$(lMaxSize, vbNullChar)
str2 = String$(lMaxSize, vbNullChar)
p = StrPtr(str1)
iStrSize = MultiByteToWideChar(65001, 0&, StrPtr(sInput), lMaxSize, p, lMaxSize)
iStrSize = WideCharToMultiByte(0, 0, p, lMaxSize, StrPtr(str2), lMaxSize, ByVal 0, 0)
iStrSize = InStr(str2, vbNullChar)
If iStrSize = 0 Then
DecodeUTF8 = str2
Else
DecodeUTF8 = Mid$(str2, 1, iStrSize - 1)
End If
End Function
На входе: СҺРҢРёРғРңРө
На выходе: С??РёР??Р
Что означает "юникод". Что не так?