+ -
当前位置:首页 → 问答吧 → 为什么读取UTF8编码的txt文本,汉字显示不全啊??

为什么读取UTF8编码的txt文本,汉字显示不全啊??

时间:2011-10-21

来源:互联网

VB code
Private Declare Function WideCharToMultiByte Lib "kernel32" (ByVal CodePage As Long, ByVal dwFlags As Long, ByVal lpWideCharStr As Long, ByVal cchWideChar As Long, ByRef lpMultiByteStr As Any, ByVal cchMultiByte As Long, ByVal lpDefaultChar As String, ByVal lpUsedDefaultChar As Long) As Long
Private Declare Function MultiByteToWideChar Lib "kernel32" (ByVal CodePage As Long, ByVal dwFlags As Long, ByVal lpMultiByteStr As Long, ByVal cchMultiByte As Long, ByVal lpWideCharStr As Long, ByVal cchWideChar As Long) As Long

Private Const CP_UTF8 = 65001




Private Function DecodeUTF8(ByVal sUtf8 As String) As String
    On Error GoTo hError
    Dim lngUtf8Size               As Long
    Dim strBuffer                   As String
    Dim lngBufferSize           As Long
    Dim lngResult                   As Long
    Dim bytUtf8()                   As Byte
    Dim n                                   As Long

    If LenB(sUtf8) = 0 Then Exit Function
    Debug.Print LenB(sUtf8)
    bytUtf8 = StrConv(sUtf8, vbFromUnicode)
    lngUtf8Size = UBound(bytUtf8) + 1
    On Error GoTo 0
    'Set   buffer   for   longest   possible   string   i.e.   each   byte   is
    'ANSI,   thus   1   unicode(2   bytes)for   every   utf-8   character.
    lngBufferSize = lngUtf8Size * 3 + 1
    strBuffer = String$(lngBufferSize, vbNullChar)
    'Translate   using   code   page   65001(UTF-8)
    lngResult = MultiByteToWideChar(CP_UTF8, 0, VarPtr(bytUtf8(0)), lngUtf8Size, _
                StrPtr(strBuffer), lngBufferSize)
    'Trim   result   to   actual   length
    If lngResult Then
        DecodeUTF8 = Left$(strBuffer, lngResult)
    End If
hFunEnd:
    Exit Function
hError:

End Function


Private Sub Command1_Click()
    Dim s As String
    DoEvents
    Open "C:\1.txt" For Input As #1
    Do While Not EOF(1)
        Line Input #1, s
        List1.AddItem DecodeUTF8(s)
    Loop
    Close #1
End Sub

C盘下的1.txt  是UTF8编码的文本文档,例如内容为:

都是

卡戴珊

使用上述代码显示的是:

?都?
卡戴?

作者: day2008   发布时间: 2011-10-21

似乎是,最终输出控件问题……

作者: yiguangqiang88   发布时间: 2011-10-21

看看这里,里面有UTF-8的处理方法
http://blog.csdn.net/supermanking/article/details/5989227

作者: SupermanKing   发布时间: 2011-10-22

热门下载

更多