当前位置:编程学习 > VB >>

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

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编码的文本文档,例如内容为:

都是

卡戴珊

使用上述代码显示的是:

?都?
卡戴?
--------------------编程问答-------------------- 似乎是,最终输出控件问题…… --------------------编程问答-------------------- 看看这里,里面有UTF-8的处理方法
http://blog.csdn.net/supermanking/article/details/5989227 --------------------编程问答-------------------- 求助~~~~~~~~~~~ --------------------编程问答--------------------
引用 3 楼 day2008 的回复:
求助~~~~~~~~~~~

我给你的帖子里有整个 UTF-8 的 VB 编码原理和过程在里面,了解了原理,还可能会出什么问题?
给了东西你都不去看,不去试,别人还怎么帮你? --------------------编程问答-------------------- 這個控件能幫助你

http://www.hexagora.com/en_dw_unictrl.asp --------------------编程问答--------------------
Private Declare Function MultiByteToWideChar Lib "kernel32" (ByVal CodePage As Long, ByVal dwFlags As Long, ByRef lpMultiByteStr As Any, ByVal cchMultiByte As Long, ByVal lpWideCharStr As Long, ByVal cchWideChar As Long) As Long
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 Long, ByVal lpUsedDefaultChar As Long) As Long
'常用的代码页:
const cpUTF8   =65001
const cpGB2312 =  936
const cpGB18030=54936
const cpUTF7   =65000
Function MultiByteToUTF16(UTF8() As Byte, CodePage As Long) As String
    Dim bufSize As Long
    bufSize = MultiByteToWideChar(CodePage, 0&, UTF8(0), UBound(UTF8) + 1, 0, 0)
    MultiByteToUTF16 = Space(bufSize)
    MultiByteToWideChar CodePage, 0&, UTF8(0), UBound(UTF8) + 1, StrPtr(MultiByteToUTF16), bufSize
End Function

Function UTF16ToMultiByte(UTF16 As String, CodePage As Long) As Byte()
    Dim bufSize As Long
    Dim arr() As Byte
    bufSize = WideCharToMultiByte(CodePage, 0&, StrPtr(UTF16), Len(UTF16), 0, 0, 0, 0)
    ReDim arr(bufSize - 1)
    WideCharToMultiByte CodePage, 0&, StrPtr(UTF16), Len(UTF16), arr(0), bufSize, 0, 0
    UTF16ToMultiByte = arr
End Function

Private Sub Command1_Click()
    MsgBox MultiByteToUTF16(UTF16ToMultiByte("ab中,c", cpUTF8), cpUTF8)
End Sub

--------------------编程问答-------------------- 用这个保证很全

http://ufo-crackerx.blog.163.com/blog/static/11307877820122119304397/ --------------------编程问答--------------------
引用 6 楼 zhao4zhong1 的回复:
VB code
Private Declare Function MultiByteToWideChar Lib "kernel32" (ByVal CodePage As Long, ByVal dwFlags As Long, ByRef lpMultiByteStr As Any, ByVal cchMultiByte As Long, ByVal lpWideCharStr As Lon……

+1 --------------------编程问答-------------------- 我說的那個才是方便的 --------------------编程问答--------------------
引用 7 楼 crackdung 的回复:
用这个保证很全

http://ufo-crackerx.blog.163.com/blog/static/11307877820122119304397/

会费的? --------------------编程问答-------------------- 晕,“付费”输成‘会费’了…… -_-!!!
--------------------编程问答-------------------- 網上很多破解,我只是告訴有這個控件,非常好。 --------------------编程问答-------------------- 楼主已于2011-10-21出国定居,后面的不要再回了。 --------------------编程问答-------------------- 原来出国就上不了csdn了 --------------------编程问答-------------------- 你信?????? --------------------编程问答-------------------- 我也遇到同样的问题
补充:VB ,  API
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,