使用StrConv只能转换utf8 里面的单字节,双字符的字符还会是乱码.
另外还有的办法能转换utf8里面的大部分字符,但是唯独双字节和单字节相连的地方,两个字符会变乱码.
如果是读取文件里面的字符,用adodb.stream处理的效果最好
代码如下,sUTF8File 表示文件的路径
使用前要先引用Microsoft ActiveX Data Objects ( ado)
Public Function readUTF8file(ByVal sUTF8File As String) As String
If Len(sUTF8File) = 0 Or Dir(sUTF8File) = vbNullString Then Exit Function
Dim ados As Object
Set ados = CreateObject("adodb.stream")
With ados
.Charset = "utf-8"
.Type = 2
.Open
.LoadFromFile sUTF8File
readUTF8file = .ReadText
.Close
End With
Set ados = Nothing
End Function
Private Const CP_ACP = 0 ' default to ANSI code page
Private Const CP_UTF8 = 65001 ' default to UTF-8 code page
Private Declare Function MultiByteToWideChar Lib "kernel32" (ByVal CodePage As Long, ByVal dwFlags As Long, ByVal lpMultiByteStr As Long, ByVal cchMultiByte As Long, ByVal lpWideCharStr As Long, ByVal cchWideChar As Long) As Long
Private Declare Function WideCharToMultiByte Lib "kernel32" (ByVal CodePage As Long, ByVal dwFlags As Long, ByVal lpWideCharStr As Long, ByVal cchWideChar As Long, ByVal lpMultiByteStr As Long, ByVal cchMultiByte As Long, ByVal lpDefaultChar As Long, ByVal lpUsedDefaultChar As Long) As Long
Private Function EncodeToBytes(ByVal sData As String) As Byte() ' Note: Len(sData) > 0
Dim aRetn() As Byte
Dim nSize As Long
nSize = WideCharToMultiByte(CP_UTF8, 0, StrPtr(sData), -1, 0, 0, 0, 0)
ReDim aRetn(0 To nSize - 1) As Byte
WideCharToMultiByte CP_UTF8, 0, StrPtr(sData), -1, VarPtr(aRetn(0)), nSize, 0, 0
EncodeToBytes = aRetn
End Function
Private Function DecodeToBytes(ByVal sData As String) As Byte() ' Note: Len(sData) > 0
Dim aRetn() As Byte
Dim nSize As Long
nSize = MultiByteToWideChar(CP_UTF8, 0, StrPtr(sData), -1, 0, 0)
ReDim aRetn(0 To 2 * nSize - 1) As Byte
MultiByteToWideChar CP_UTF8, 0, StrPtr(sData), -1, VarPtr(aRetn(0)), nSize
DecodeToBytes = aRetn
End Function
Private Sub Command1_Click()
Dim a(), i As Long
Open "c:\a.txt" For Input As #1
Do While Not EOF(1)
ReDim Preserve a(i)
Input #1, a(i)
i = i + 1
Loop
Close #1
MsgBox a(0)
MsgBox DecodeToBytes(StrConv(a(0), vbFromUnicode))
End Sub
资料:
http://topic.csdn.net/t/20060925/11/5045535.html
http://topic.csdn.net/t/20051002/15/4305498.html
http://topic.csdn.net/t/20050504/19/3983484.html
http://www.google.com/search?q=VB+UTF-8
你好,此问题已作回答,答案在http://zhidao.baidu.com/question/123224632.html?fr=im2。