- 生肖
- 猴
- 星座
- 处女座
- 性别
- 男
- 积分
- 680
- 积分
- 1161
- 精华
- 1
- 阅读权限
- 70
- 注册时间
- 2012-5-3
- 最后登录
- 2016-9-22
- 帖子
- 136
- 生肖
- 猴
- 星座
- 处女座
- 性别
- 男
|
本帖最后由 sky_yx 于 2015-12-30 14:23 编辑
- Option Explicit
- Private Declare Function MultiByteToWideChar Lib "kernel32" (ByVal CodePage As Long, ByVal dwFlags As Long, ByRef lpMultiByteStr As Any, ByVal cchMultiByte As Long, ByVal lpWideCharStr As Long, ByVal cchWideChar As Long) As Long
- Private Const CP_UTF8 = 65001
- '读文件至变量
- Private Function GetFile(FileName As String) As String
- Dim i As Integer, 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
- GetFile = BB
- End Function
- '功能: 把Utf8字符转化成ANSI字符
- Public Function UTF8_Decode(FileName As String) As String
- Dim sUTF8 As String
- Dim lngUtf8Size As Long
- Dim strBuffer As String
- Dim lngBufferSize As Long
- Dim lngResult As Long
- Dim bytUtf8() As Byte
- Dim n As Long
- sUTF8 = GetFile(FileName)
- If LenB(sUTF8) = 0 Then Exit Function
- On Error GoTo EndFunction
- bytUtf8 = sUTF8
- lngUtf8Size = UBound(bytUtf8) + 1
- lngBufferSize = lngUtf8Size * 2
- strBuffer = String$(lngBufferSize, vbNullChar)
- lngResult = MultiByteToWideChar(CP_UTF8, 0, bytUtf8(0), _
- lngUtf8Size, StrPtr(strBuffer), lngBufferSize)
- If lngResult Then
- UTF8_Decode = Left(strBuffer, lngResult)
- End If
- EndFunction:
-
- End Function
复制代码 二、调用举例:
如果你想把一个"c:\1.txt"的UTF-8文件转换为ANSI编码,可这样调用
dim s as string
s=UTF8_Decode("c:\1.txt") '文件名请根据实际修改
此时,s存放的就是ANSI格式编码了,不会出现乱码问题锦葵科
下面的是ansi转换utf-8- Private Sub Command4_Click()s = "C:\Documents and Settings\Administrator\桌面\素材\2.xml"
- ToUtf8 s, "c:\ab.xml"
- MsgBox "ok"
- End Sub
- Private Sub ToUtf8(ByVal s As String, ByVal FilePath As String)
- Dim stmStr
- Set stmStr = CreateObject("ADODB.Stream")
- stmStr.Open
- stmStr.Charset = "utf-8"
- stmStr.WriteText s
- stmStr.SaveToFile FilePath
- stmStr.Close
- Set stmStr = Nothing
- End Sub
复制代码
|
|