utf-8转unicode,以下代码虽然能转换,但是还有部份文字依然是乱码。请帮忙,
要把所有的都转换成功。Private Sub Command1_Click()
Dim s As Stream
Set s = New Stream
s.Open
s.Charset = "utf-8"
s.Position = 0
s.LoadFromFile "C:\z.txt"
s.Position = 0
Text1 = s.ReadText
End Sub

解决方案 »

  1.   

    'Utf8 转换为 UnicodePublic Function UTF8_Decode(ByVal s As String) As String Dim lUtf8Size As LongDim sBuffer As StringDim lBufferSize As LongDim lResult As LongDim b() As ByteIf LenB(s) ThenOn Error GoTo EndFunctionb = StrConv(s, vbFromUnicode)lUtf8Size = UBound(b) + 1On Error GoTo 0'Set buffer for longest possible string i.e. each byte is'ANSI<=&HFF, thus 1 unicode(2 bytes)for every utf-8 character.lBufferSize = lUtf8Size * 2sBuffer = String$(lBufferSize, vbNullChar)'Translate using code page 65001(UTF-8)lResult = MultiByteToWideChar(CP_UTF8, 0, b(0), _lUtf8Size, StrPtr(sBuffer), lBufferSize)'Trim result to actual lengthIf lResult ThenUTF8_Decode = Left$(sBuffer, lResult)'Debug.Print UTF8_DecodeEnd IfEnd IfEndFunction:End Function'Unicode转换为UTF-8.Public Function UTF8_Encode(ByVal strUnicode As String) As String 'ByVal strUnicode As ByteDim TLen As LongTLen = Len(strUnicode)If TLen = 0 Then Exit FunctionDim lBufferSize As LongDim lResult As LongDim b() As Byte'Set buffer for longest possible string.lBufferSize = TLen * 3 + 1ReDim b(lBufferSize - 1)'Translate using code page 65001(UTF-8).lResult = WideCharToMultiByte(CP_UTF8, 0, StrPtr(strUnicode), _TLen, b(0), lBufferSize, vbNullString, 0)'Trim result to actual length.If lResult ThenlResult = lResult - 1ReDim Preserve b(lResult)UTF8_Encode = StrConv(b, vbUnicode)End IfEnd Function
      

  2.   

    Dim fnmi As String
    Dim fi As Integer
    Dim fnmo As String
    Dim fo As Integer
    Dim s() As Byte
    Dim t() As String * 1
    Dim L As Long
    Dim i As Long
    Dim j As Long
        fnmo="输入文件名(UTF8)"
        fnmi="输出文件名(ANSI)"
        fo = FreeFile()
        Open fnmo For Output As #fo
        fi = FreeFile()
        Open fnmi For Binary As #fi
        L = LOF(fi)
        ReDim s(L)
        Get #fi, , s
        ReDim Preserve t(L)
        j = 0
        For i = 0 To L
            If s(i) < 128 Then
                t(j) = Chr(s(i)): j = j + 1
            ElseIf s(i) < 224 Then
                t(j) = ChrB((s(i) And 3) * 64 + (s(i + 1) And 63)) + ChrB((s(i) And 28) / 4): j = j + 1
                i = i + 1
            Else
                t(j) = ChrB((s(i + 1) And 3) * 64 + (s(i + 2) And 63)) + ChrB((s(i) And 15) * 16 + (s(i + 1) And 60) / 4): j = j + 1
                i = i + 2
            End If
        Next
        For i = 0 To j - 1
            Print #fo, t(i);
        Next
        Close #fi
        Close #fo