高分求VB读取文本乱码问题utf-8和ansi互相转换
我用VB读取一他TXT文本,编码为UTF-8,读取入变量,显示为乱码然后将文本另存为ANSI格式编码时,读取却正常但是我想让VB直接读取UTF格式编码文件,不想每次都要把...
我用VB读取一他TXT文本,编码为UTF-8,读取入变量,显示为乱码
然后将文本另存为ANSI格式编码时,读取却正常
但是我想让VB直接读取UTF格式编码文件,不想每次都要把文件另存为ANSI格式的文件(因为文件很多)
求高人帮下忙
我的用VB读入是打开文本然后把每行赋值给A(i),我想用一个函数或过程来把A(i)转换成ANSI格式的,比如
OPen "c\a.txt" for input as #1
do while not eof(1)
redim preserve a(i)
line print #1,a(i)
a(i)=(utf-8转ansi的过程)(a(i))
i=i+1
loop
close# 1
希望高手写个代码~~
编辑完成之后还要转换回去,希望在写个ANSI转UFT-8的代码过程
例如
Private Sub cmdsave_Click()
Dim j As Integer
Open "a.txt" For Output As #1
For j = 0 To UBound(a)
Print #1, a(j)
a(i)=(ansi 转utf-8的代码)(a(i))
Next
Close #1
End Sub
完成后追加100分,多谢!!! 展开
然后将文本另存为ANSI格式编码时,读取却正常
但是我想让VB直接读取UTF格式编码文件,不想每次都要把文件另存为ANSI格式的文件(因为文件很多)
求高人帮下忙
我的用VB读入是打开文本然后把每行赋值给A(i),我想用一个函数或过程来把A(i)转换成ANSI格式的,比如
OPen "c\a.txt" for input as #1
do while not eof(1)
redim preserve a(i)
line print #1,a(i)
a(i)=(utf-8转ansi的过程)(a(i))
i=i+1
loop
close# 1
希望高手写个代码~~
编辑完成之后还要转换回去,希望在写个ANSI转UFT-8的代码过程
例如
Private Sub cmdsave_Click()
Dim j As Integer
Open "a.txt" For Output As #1
For j = 0 To UBound(a)
Print #1, a(j)
a(i)=(ansi 转utf-8的代码)(a(i))
Next
Close #1
End Sub
完成后追加100分,多谢!!! 展开
3个回答
展开全部
使用StrConv只能转换utf8 里面的单字节,双字符的字符还会是乱码.
另外还有的办法能转换utf8里面的大部分字符,但是唯独双字节和单字节相连的地方,两个字符会变乱码.
如果是读取文件里面的字符,用adodb.stream处理的效果最好
代码如下,sUTF8File 表示文件的路径
使用前要先引用Microsoft ActiveX Data Objects ( ado)
Public Function readUTF8file(ByVal sUTF8File As String) As String
If Len(sUTF8File) = 0 Or Dir(sUTF8File) = vbNullString Then Exit Function
Dim ados As Object
Set ados = CreateObject("adodb.stream")
With ados
.Charset = "utf-8"
.Type = 2
.Open
.LoadFromFile sUTF8File
readUTF8file = .ReadText
.Close
End With
Set ados = Nothing
End Function
另外还有的办法能转换utf8里面的大部分字符,但是唯独双字节和单字节相连的地方,两个字符会变乱码.
如果是读取文件里面的字符,用adodb.stream处理的效果最好
代码如下,sUTF8File 表示文件的路径
使用前要先引用Microsoft ActiveX Data Objects ( ado)
Public Function readUTF8file(ByVal sUTF8File As String) As String
If Len(sUTF8File) = 0 Or Dir(sUTF8File) = vbNullString Then Exit Function
Dim ados As Object
Set ados = CreateObject("adodb.stream")
With ados
.Charset = "utf-8"
.Type = 2
.Open
.LoadFromFile sUTF8File
readUTF8file = .ReadText
.Close
End With
Set ados = Nothing
End Function
参考资料: http://zhidao.baidu.com/question/51351253.html?si=3
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Private Const CP_ACP = 0 ' default to ANSI code page
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 Declare Function WideCharToMultiByte Lib "kernel32" (ByVal CodePage As Long, ByVal dwFlags As Long, ByVal lpWideCharStr As Long, ByVal cchWideChar As Long, ByVal lpMultiByteStr As Long, ByVal cchMultiByte As Long, ByVal lpDefaultChar As Long, ByVal lpUsedDefaultChar As Long) As Long
Private Function EncodeToBytes(ByVal sData As String) As Byte() ' Note: Len(sData) > 0
Dim aRetn() As Byte
Dim nSize As Long
nSize = WideCharToMultiByte(CP_UTF8, 0, StrPtr(sData), -1, 0, 0, 0, 0)
ReDim aRetn(0 To nSize - 1) As Byte
WideCharToMultiByte CP_UTF8, 0, StrPtr(sData), -1, VarPtr(aRetn(0)), nSize, 0, 0
EncodeToBytes = aRetn
End Function
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 a(), i As Long
Open "c:\a.txt" For Input As #1
Do While Not EOF(1)
ReDim Preserve a(i)
Input #1, a(i)
i = i + 1
Loop
Close #1
MsgBox a(0)
MsgBox DecodeToBytes(StrConv(a(0), vbFromUnicode))
End Sub
资料:
http://topic.csdn.net/t/20060925/11/5045535.html
http://topic.csdn.net/t/20051002/15/4305498.html
http://topic.csdn.net/t/20050504/19/3983484.html
http://www.google.com/search?q=VB+UTF-8
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 Declare Function WideCharToMultiByte Lib "kernel32" (ByVal CodePage As Long, ByVal dwFlags As Long, ByVal lpWideCharStr As Long, ByVal cchWideChar As Long, ByVal lpMultiByteStr As Long, ByVal cchMultiByte As Long, ByVal lpDefaultChar As Long, ByVal lpUsedDefaultChar As Long) As Long
Private Function EncodeToBytes(ByVal sData As String) As Byte() ' Note: Len(sData) > 0
Dim aRetn() As Byte
Dim nSize As Long
nSize = WideCharToMultiByte(CP_UTF8, 0, StrPtr(sData), -1, 0, 0, 0, 0)
ReDim aRetn(0 To nSize - 1) As Byte
WideCharToMultiByte CP_UTF8, 0, StrPtr(sData), -1, VarPtr(aRetn(0)), nSize, 0, 0
EncodeToBytes = aRetn
End Function
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 a(), i As Long
Open "c:\a.txt" For Input As #1
Do While Not EOF(1)
ReDim Preserve a(i)
Input #1, a(i)
i = i + 1
Loop
Close #1
MsgBox a(0)
MsgBox DecodeToBytes(StrConv(a(0), vbFromUnicode))
End Sub
资料:
http://topic.csdn.net/t/20060925/11/5045535.html
http://topic.csdn.net/t/20051002/15/4305498.html
http://topic.csdn.net/t/20050504/19/3983484.html
http://www.google.com/search?q=VB+UTF-8
参考资料: http://topic.csdn.net/t/20060925/11/5045535.html
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你好,此问题已作回答,答案在http://zhidao.baidu.com/question/123224632.html?fr=im2。
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询