VB 读取 UTF-8 问题

OptionExplicitPrivateConstCP_UTF8=65001'defaulttoUTF-8codepagePrivateDeclareFunctionM... Option Explicit
Private Const CP_UTF8 = 65001 ' default to UTF-8 code page
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 Function DecodeToBytes(ByVal sData As String) As Byte() ' Note: Len(sData) > 0
Dim aRetn() As Byte
Dim nSize As Long

nSize = MultiByteToWideChar(CP_UTF8, 0, StrPtr(sData), -1, 0, 0)
ReDim aRetn(0 To 2 * nSize - 1) As Byte
MultiByteToWideChar CP_UTF8, 0, StrPtr(sData), -1, VarPtr(aRetn(0)), nSize

DecodeToBytes = aRetn
End Function

Private Sub Command1_Click()
Dim fContents As String
Dim fContents0 As String
Open "C:\pppp.wap" For Input Access Read As #1
Do Until EOF(1)
Line Input #1, fContents0
fContents = fContents + fContents0
Loop
Close #1
Text1 = DecodeToBytes(StrConv(fContents, vbFromUnicode))
End Sub

上面代码读取pppp.wap这个UTF-8码的文件,但它读取后有某些中文还是显示为“?”号,为什么 呢?要怎样改才能正常显示。
其实我是写在模块的,这里写在Private Sub Command1_Click()只是测试用,而已没有microsoft activex data object library。不能引用microsoft activex data object library啊~~~
展开
 我来答
微软专家张海
2009-09-08 · TA获得超过748个赞
知道小有建树答主
回答量:564
采纳率:100%
帮助的人:0
展开全部
引用microsoft activex data object library
Dim s As Stream
Set s = New Stream
s.Open
s.Charset = "UTF-8"
s.Position = 0
s.LoadFromFile "C:\pppp.wap"
s.Position = 0

Text1 = s.ReadText
匿名用户

2009-09-08
展开全部
Private Sub Form_Load()
Dim objStream As New ADODB.Stream
Dim str As String

With objStream
.Type = adTypeText
.Mode = 3
.Mode = 3
.Open
.LoadFromFile "c:\test.txt"
.Position = 0
.Charset = "UTF-8"
str = .ReadText
.Close
End With
MsgBox str
End Sub

'引用microsoft activex data object library 2.5
安装VB6.0企业版
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式