为什么读取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
http://blog.csdn.net/supermanking/article/details/5989227
作者: SupermanKing 发布时间: 2011-10-22
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28