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啊~~~ 展开
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啊~~~ 展开
2个回答
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企业版
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企业版
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询