用VB读取unicode 编码的中英文字符混合的文本文件,中文显示乱码怎么解决?
不能把原文本文件转换为ANSI格式,因为含有特殊德语字符,ANSI没有这类字符。采用StrConv也不管用,转换完还是乱码。...
不能把原文本文件转换为ANSI格式,因为含有特殊德语字符,ANSI没有这类字符。
采用StrConv也不管用,转换完还是乱码。 展开
采用StrConv也不管用,转换完还是乱码。 展开
3个回答
展开全部
用API函数,以下是在CSDN搜到的,自己调试了下,可以
--------------------------------------------------
Option Explicit
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, source As Any, ByVal Length As Long)
Private Function ReadUniFile(ByVal sFile As String) As String
'没加错误处理,大家自己加吧
Dim a As Long
a = FileLen(sFile)
ReDim buff(a - 1) As Byte
ReDim buff1(a - 3) As Byte
Open sFile For Binary As #1
Get #1, , buff
Close #1
CopyMemory buff1(0), buff(2), a - 2
Dim s As String
s = StrConv(buff1, vbNarrow)
ReadUniFile = s
End Function
Private Sub Command1_Click()
Dim str As String
str = ReadUniFile(App.Path + "\12.txt")
Text1.Text = str
End Sub
--------------------------------------------------
Option Explicit
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, source As Any, ByVal Length As Long)
Private Function ReadUniFile(ByVal sFile As String) As String
'没加错误处理,大家自己加吧
Dim a As Long
a = FileLen(sFile)
ReDim buff(a - 1) As Byte
ReDim buff1(a - 3) As Byte
Open sFile For Binary As #1
Get #1, , buff
Close #1
CopyMemory buff1(0), buff(2), a - 2
Dim s As String
s = StrConv(buff1, vbNarrow)
ReadUniFile = s
End Function
Private Sub Command1_Click()
Dim str As String
str = ReadUniFile(App.Path + "\12.txt")
Text1.Text = str
End Sub
参考资料: http://blog.csdn.net/rainstormmaster/archive/2007/05/03/1595322.aspx
展开全部
如果是 一般Unicode 可以(UTF-8 复杂多)
Unicode 文件首两字节应该是 0xFF(&HFF) 0xFE (&HFE)
每两个字节读一次 用ChrW() 转为字符
Unicode 文件首两字节应该是 0xFF(&HFF) 0xFE (&HFE)
每两个字节读一次 用ChrW() 转为字符
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
人工转换……
open "[path]" for binary as #1
二进制一个一个读……
open "[path]" for binary as #1
二进制一个一个读……
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询