VB中如何生成编码格式为UTF-8的文本文件?
3个回答
展开全部
'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
'第一个参数:指定要转换成的字符集代码页,它可以是任何已经安装的或系统自带的字符集,你也可以使用如下所示代码页之一。
' 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
展开全部
跟你text1.text设置的语言有关系,你文本中保存的阿拉伯语,那么text1.text设置的语言也要是阿拉伯语
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
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
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百度之即可
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询