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
展开
 我来答
ljl88900
推荐于2016-06-05 · TA获得超过2660个赞
知道大有可为答主
回答量:2197
采纳率:100%
帮助的人:2612万
展开全部
抱歉,你的消息今晚才看到,现补上。
分二步:
一、新建一个模块,复制下面代码:
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编码。
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
tn瓶邪
推荐于2018-03-30 · TA获得超过1881个赞
知道小有建树答主
回答量:610
采纳率:98%
帮助的人:111万
展开全部
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
本回答被网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式