VB中MultiByteToWideChar如何调用使得文本格式由ANSI转换成UTF-8
F盘的TEMP文件夹里有一个A.TXT,其编码为ansi,希望高手帮写个程序,把该文本转换成UFT-8的编码要求该函数或过程传入的参数是文件的路径,比如传入"f:\tem...
F盘的TEMP文件夹里有一个A.TXT,其编码为ansi,希望高手帮写个程序,把该文本转换成UFT-8的编码
要求该函数或过程传入的参数是文件的路径,比如传入"f:\temp\a.txt"
然后就会将f:\temp\a.txt转换成UTF-8编码格式的文件,替换掉原先的a.txt
注意,是用MultiByteToWideChar函数,因为我把UTF-8转换成ANSI就是用这个函数,现在想转回去,即ANSI转UTF-8,用其他方法转的话是可以,但是容易出错
成功后追加100分,不胜感激!
好像转回来是用这个吧WideCharToMultiByte 展开
要求该函数或过程传入的参数是文件的路径,比如传入"f:\temp\a.txt"
然后就会将f:\temp\a.txt转换成UTF-8编码格式的文件,替换掉原先的a.txt
注意,是用MultiByteToWideChar函数,因为我把UTF-8转换成ANSI就是用这个函数,现在想转回去,即ANSI转UTF-8,用其他方法转的话是可以,但是容易出错
成功后追加100分,不胜感激!
好像转回来是用这个吧WideCharToMultiByte 展开
2个回答
展开全部
抱歉,你的消息今晚才看到,现补源磨瞎上。
分二步:
一、新建一个模块,复制下面代码:
Option Explicit
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 Const CP_UTF8 = 65001
'读文件至变量
Function GetFile(Filename As String) As String
Dim i As Integer, S As String, BB() As Byte
If Dir(Filename) = "" Then Exit Function
i = FreeFile
ReDim BB(FileLen(Filename) - 1)
Open Filename For Binary As #i
Get #i, , BB
Close #i
S = BB
GetFile = S
End Function
'写游兄入文件1
Public Function PutFile(Filename As String, txt As String)
If txt = "" Then Exit Function
If Dir(Filename) <> "" Then Kill Filename
Dim i As Integer, S As String, BB() As Byte
i = FreeFile
Open Filename For Binary As #i
Put #i, , txt
Close #i
End Function
Public Function UTF8_Encode(Filename As String) As Boolean
Dim i As Long
Dim TLen As Long
Dim lPtr As Long
Dim UTF16 As Long
Dim UTF8_EncodeLong As String
Dim strUnicode As String, S As String
If Dir(Filename) = "" Or Filename = "" Then Exit Function
strUnicode = GetFile(Filename)
strUnicode = StrConv(strUnicode, vbUnicode)
TLen = Len(strUnicode)
If TLen = 0 Then Exit Function
Dim lngBufferSize As Long
Dim lngResult As Long
Dim bytUtf8() As Byte
lngBufferSize = TLen * 3 + 1
ReDim bytUtf8(lngBufferSize - 1)
lngResult = WideCharToMultiByte(CP_UTF8, 0, StrPtr(strUnicode), _
TLen, bytUtf8(0), lngBufferSize, vbNullString, 0)
If lngResult Then
lngResult = lngResult - 1
ReDim Preserve bytUtf8(lngResult)
S = StrConv(bytUtf8, vbUnicode)
End If
PutFile Filename, S
UTF8_Encode = True
End Function
二、程序调雹空用如下:
If UTF8_Encode("f:\temp\a.txt") Then
MsgBox ""文件转换成功!"
Else
MsgBox "文件转换失败!"
End If
注意:转换成功后的文件将被覆盖,直接变成UTF-8编码。
分二步:
一、新建一个模块,复制下面代码:
Option Explicit
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 Const CP_UTF8 = 65001
'读文件至变量
Function GetFile(Filename As String) As String
Dim i As Integer, S As String, BB() As Byte
If Dir(Filename) = "" Then Exit Function
i = FreeFile
ReDim BB(FileLen(Filename) - 1)
Open Filename For Binary As #i
Get #i, , BB
Close #i
S = BB
GetFile = S
End Function
'写游兄入文件1
Public Function PutFile(Filename As String, txt As String)
If txt = "" Then Exit Function
If Dir(Filename) <> "" Then Kill Filename
Dim i As Integer, S As String, BB() As Byte
i = FreeFile
Open Filename For Binary As #i
Put #i, , txt
Close #i
End Function
Public Function UTF8_Encode(Filename As String) As Boolean
Dim i As Long
Dim TLen As Long
Dim lPtr As Long
Dim UTF16 As Long
Dim UTF8_EncodeLong As String
Dim strUnicode As String, S As String
If Dir(Filename) = "" Or Filename = "" Then Exit Function
strUnicode = GetFile(Filename)
strUnicode = StrConv(strUnicode, vbUnicode)
TLen = Len(strUnicode)
If TLen = 0 Then Exit Function
Dim lngBufferSize As Long
Dim lngResult As Long
Dim bytUtf8() As Byte
lngBufferSize = TLen * 3 + 1
ReDim bytUtf8(lngBufferSize - 1)
lngResult = WideCharToMultiByte(CP_UTF8, 0, StrPtr(strUnicode), _
TLen, bytUtf8(0), lngBufferSize, vbNullString, 0)
If lngResult Then
lngResult = lngResult - 1
ReDim Preserve bytUtf8(lngResult)
S = StrConv(bytUtf8, vbUnicode)
End If
PutFile Filename, S
UTF8_Encode = True
End Function
二、程序调雹空用如下:
If UTF8_Encode("f:\temp\a.txt") Then
MsgBox ""文件转换成功!"
Else
MsgBox "文件转换失败!"
End If
注意:转换成功后的文件将被覆盖,直接变成UTF-8编码。
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Private Sub AnsiToUtf8 (FileName as String)
Dim b() As Byte, S As String
Dim ADO_Stream As Object
If Dir(FileName) = "" Then Exit Sub
ReDim b(FileLen(FileName))
Open FileName For Binary As #1
Get #1, , b
Close #1
S = StrConv(b, vbUnicode)
Set ADO_Stream = CreateObject("ADODB.Stream")
With ADO_Stream
.Type = 2
.Mode = 3
.Charset = "utf-8"
.open
.WriteText S
.SaveToFile FileName, 2
End With
Set ADO_Stream = Nothing
End Sub
参考资料: http://zhidao.baidu.com/question/83609963.html?si=3
Dim b() As Byte, S As String
Dim ADO_Stream As Object
If Dir(FileName) = "" Then Exit Sub
ReDim b(FileLen(FileName))
Open FileName For Binary As #1
Get #1, , b
Close #1
S = StrConv(b, vbUnicode)
Set ADO_Stream = CreateObject("ADODB.Stream")
With ADO_Stream
.Type = 2
.Mode = 3
.Charset = "utf-8"
.open
.WriteText S
.SaveToFile FileName, 2
End With
Set ADO_Stream = Nothing
End Sub
参考资料: http://zhidao.baidu.com/question/83609963.html?si=3
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询