VB中如何生成编码格式为UTF-8的文本文件?

如题所述

'API 函数WideCharToMultiByte参数说明
'第一个参数:指定要转换成的字符集代码页,它可以是任何已经安装的或系统自带的字符集,你也可以使用如下所示代码页之一。
' CP_ACP 当前系统ANSI代码页
' CP_MACCP 当前系统Macintosh代码页
' CP_OEMCP 当前系统OEM代码页,一种原始设备制造商硬件扫描码
' CP_SYMBOL Symbol代码页.
' CP_THREAD_ACP 当前线程ANSI代码页,用于Windows 2000及以后版本,我不明白是什么
' CP_UTF7 UTF-7,设置此值时lpDefaultChar和lpUsedDefaultChar都必须为NULL
' CP_UTF8 UTF-8,设置此值时lpDefaultChar和lpUsedDefaultChar都必须为NULL
'第二个参数:指定如何处理没有转换的字符,但不设此参数函数会运行的更快一些,我都是把它设为0。
'第三个参数: 待转换的宽字符串?
'第四个参数:待转换宽字符串的长度,-1表示转换到字符串结尾。
'第五个参数: 接收转换后输出新串的缓冲区?
'第六个参数: 输出缓冲区大小?
'第七个参数: 指向字符的指针?
'第八个参数:开关变量的指针,用以表明是否使用过默认字符,一般设为0。
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
'

Sub test()
strTest = "这是文件内容"
testFile = "d:\gb2312.html"
Call SaveFile(testFile, strTest, "gb2312")
testFile = "d:\utf8.html"
Call SaveFile(testFile, strTest, "utf8")
End Sub
Sub SaveFile(strPath_file, str, strPageCode)
If strPageCode = "utf8" Then
'存为UTF-8
Dim lBufSize As Long
Dim lRest As Long
Dim bUTF8() As Byte
Dim TLen As Long
'
TLen = Len(str)
lBufSize = TLen * 3 + 1
ReDim bUTF8(lBufSize - 1)
lRest = WideCharToMultiByte(CP_UTF8, 0, StrPtr(str), TLen, bUTF8(0), lBufSize, vbNullString, 0)
If lRest Then
lRest = lRest - 1
ReDim Preserve bUTF8(lRest)
Open strPath_file For Binary As #1
Put #1, , bUTF8
Close #1
End If
Else
'存为GB-2312
Fn = FreeFile
Open strPath_file For Output As Fn
Print #Fn, str
Close #Fn
End If
End Sub
温馨提示:内容为网友见解,仅供参考
第1个回答  2020-03-25
跟你text1.text设置的语言有关系,你文本中保存的阿拉伯语,那么text1.text设置的语言也要是阿拉伯语
第2个回答  2011-08-08
Private Declare Function icePub_gbkToUTF8 Lib "icePubDll.dll" (ByVal strGbkInfo As String,ByVal strUtf8Info As String,ByVal strMaxLen As Integer) As Integer

Dim str1 As String
Dim a2 As Integer

str1=Space(1024+1)

a2=icePub_gbkToUTF8("月光",str1,128)

MsgBox str1追问

编译错误:在End Sub、End Function或End属性后面只能出现注释
End Sub后面明明没有文字了啊

追答

Private Declare Function icePub_gbkToUTF8 Lib "icePubDll.dll" (ByVal strGbkInfo As String, ByVal strUtf8Info As String, ByVal strMaxLen As Integer) As Integer

Private Sub Form_Load()

Dim str1 As String
Dim a2 As Integer

str1 = Space(1024 + 1)

a2 = icePub_gbkToUTF8("月光", str1, 128)

MsgBox str1

End Sub

然后
http://114.246.67.4/dev/icePubDll.rar
下新版

参考资料:icePubDll.dll百度之即可

VB中如何生成编码格式为UTF-8的文本文件?
'第一个参数:指定要转换成的字符集代码页,它可以是任何已经安装的或系统自带的字符集,你也可以使用如下所示代码页之一。' CP_ACP 当前系统ANSI代码页 ' CP_MACCP 当前系统Macintosh代码页 ' CP_OEMCP 当前系统OEM代码页,一种原始设备制造商硬件扫描码 ' CP_SYMBOL Symbol代码页.' ...

VB 建UTF-8的文本文件
VB编程下用UTF-8编码编写的文本文件,都会产生一个BOM,这使得转换出来的文件,在系统编译中编译不过去,必须要把BOM去掉,通过代码实现如下:Dim app As Excel.ApplicationDim eworkbook As WorkbookDim eworksheet As WorksheetDim eworksheet_count As IntegerDim sheetName As StringDim obj As ObjectD...

VB输出UTF-8编码的TXT文件?
Function SaveTextAsUTF8(filePath, Text)Const adTypeText = 2 Const adSaveCreateOverWrite = 2 'Create Stream object Dim TextStream Set TextStream = CreateObject("ADODB.Stream")With TextStream .Open .CharSet = "UTF-8".Position = TextStream.Size .WriteText Text .SaveToFile filePath, a...

VB 怎么更改TXT编码格式和读取Txt指定字符
VB可以通过CreateObject函数来实现打开UTF-8编码格式文本文件,将它显示于文本框中。并可通过Print # 语句将格式化显示的数据以ANSI格式写入顺序文件中。CreateObject函数,创建并返回一个对 ActiveX 对象的引用。Print # 语句,将格式化显示的数据写入顺序文件中。以下是实现代码:Private Sub Command1_Click()...

VB 如何将TEXT保存为utf8格式的文件
End If Else iUnicode = iTemp End If sResult = sResult & U2UTF8(iUnicode)Next GB2UTF = sResult End Function Private Sub Form_Load()Dim utf8() As Byte utf8 = U2UTF8("hello")Open "d:\\a.txt" For Binary As #1 Put #1, , utf8 Close #1 End Sub 这样即可!!

vb中如何将字符串转换为utf-8编码
Function UTF(ZZF)Dim ZTEP, I, ZA, ZC UTF = ""ZTEP = ZZF & ""If ZTEP = "" Then Exit Function For I = 1 To Len(ZTEP)ZA = Mid(ZTEP, I, 1)ZC = Asc(ZA)If ZC > 127 Or ZC < 0 Then ZC = AscW(ZA)If ZC < 0 Then ZC = &H10000 + ZC If ZC < 2048 Then ...

vb中怎么把ansi转换成UTF-8
#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 MsgBox "文件转换完毕!"End Sub ...

VB怎么把ANSI编码的txt文件转为UTF-8编码?
charset设置为utf-8即可 Function BytesToBstr(body, charset)Dim objstream Set objstream = CreateObject("adodb.stream")objstream.Type = 1 objstream.Mode = 3 objstream.Open On Error Resume Next objstream.Write body objstream.Position = 0 objstream.Type = 2 objstream.charset = charset...

vb6中如何把一个汉字或英文转换成utf8的编码???
.版本 2 .支持库 iconv 转换编码 = 编码转换 (到字节集 (当前帐户), #编码_GB2312, #编码_UTF_8, )输出调试文本 (到文本 (转换编码)).计次循环首 (取字节集长度 (转换编码), 计次)字节 = 到字节 (取字节集数据 (转换编码, 3, 计次))' 输出调试文本 (取字节集数据 (转换编码, 3...

VB中MultiByteToWideChar如何调用使得文本格式由ANSI转换成UTF-8
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编码。

相似回答