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 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 LongPrivate Const CP_UTF8 = 65001
Private Function DecodeUTF8(ByVal sUtf8 As String) As String
On Error GoTo hError
Dim lngUtf8Size As Long
Dim strBuffer As String
Dim lngBufferSize As Long
Dim lngResult As Long
Dim bytUtf8() As Byte
Dim n As Long If LenB(sUtf8) = 0 Then Exit Function
Debug.Print LenB(sUtf8)
bytUtf8 = StrConv(sUtf8, vbFromUnicode)
lngUtf8Size = UBound(bytUtf8) + 1
On Error GoTo 0
'Set buffer for longest possible string i.e. each byte is
'ANSI, thus 1 unicode(2 bytes)for every utf-8 character.
lngBufferSize = lngUtf8Size * 3 + 1
strBuffer = String$(lngBufferSize, vbNullChar)
'Translate using code page 65001(UTF-8)
lngResult = MultiByteToWideChar(CP_UTF8, 0, VarPtr(bytUtf8(0)), lngUtf8Size, _
StrPtr(strBuffer), lngBufferSize)
'Trim result to actual length
If lngResult Then
DecodeUTF8 = Left$(strBuffer, lngResult)
End If
hFunEnd:
Exit Function
hError:End Function
Private Sub Command1_Click()
Dim s As String
DoEvents
Open "C:\1.txt" For Input As #1
Do While Not EOF(1)
Line Input #1, s
List1.AddItem DecodeUTF8(s)
Loop
Close #1
End SubC盘下的1.txt 是UTF8编码的文本文档,例如内容为:都是卡戴珊使用上述代码显示的是:?都?
卡戴?
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 LongPrivate Const CP_UTF8 = 65001
Private Function DecodeUTF8(ByVal sUtf8 As String) As String
On Error GoTo hError
Dim lngUtf8Size As Long
Dim strBuffer As String
Dim lngBufferSize As Long
Dim lngResult As Long
Dim bytUtf8() As Byte
Dim n As Long If LenB(sUtf8) = 0 Then Exit Function
Debug.Print LenB(sUtf8)
bytUtf8 = StrConv(sUtf8, vbFromUnicode)
lngUtf8Size = UBound(bytUtf8) + 1
On Error GoTo 0
'Set buffer for longest possible string i.e. each byte is
'ANSI, thus 1 unicode(2 bytes)for every utf-8 character.
lngBufferSize = lngUtf8Size * 3 + 1
strBuffer = String$(lngBufferSize, vbNullChar)
'Translate using code page 65001(UTF-8)
lngResult = MultiByteToWideChar(CP_UTF8, 0, VarPtr(bytUtf8(0)), lngUtf8Size, _
StrPtr(strBuffer), lngBufferSize)
'Trim result to actual length
If lngResult Then
DecodeUTF8 = Left$(strBuffer, lngResult)
End If
hFunEnd:
Exit Function
hError:End Function
Private Sub Command1_Click()
Dim s As String
DoEvents
Open "C:\1.txt" For Input As #1
Do While Not EOF(1)
Line Input #1, s
List1.AddItem DecodeUTF8(s)
Loop
Close #1
End SubC盘下的1.txt 是UTF8编码的文本文档,例如内容为:都是卡戴珊使用上述代码显示的是:?都?
卡戴?
http://blog.csdn.net/supermanking/article/details/5989227
给了东西你都不去看,不去试,别人还怎么帮你?
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 Long, ByVal lpUsedDefaultChar As Long) As Long
'常用的代码页:
const cpUTF8 =65001
const cpGB2312 = 936
const cpGB18030=54936
const cpUTF7 =65000
Function MultiByteToUTF16(UTF8() As Byte, CodePage As Long) As String
Dim bufSize As Long
bufSize = MultiByteToWideChar(CodePage, 0&, UTF8(0), UBound(UTF8) + 1, 0, 0)
MultiByteToUTF16 = Space(bufSize)
MultiByteToWideChar CodePage, 0&, UTF8(0), UBound(UTF8) + 1, StrPtr(MultiByteToUTF16), bufSize
End FunctionFunction UTF16ToMultiByte(UTF16 As String, CodePage As Long) As Byte()
Dim bufSize As Long
Dim arr() As Byte
bufSize = WideCharToMultiByte(CodePage, 0&, StrPtr(UTF16), Len(UTF16), 0, 0, 0, 0)
ReDim arr(bufSize - 1)
WideCharToMultiByte CodePage, 0&, StrPtr(UTF16), Len(UTF16), arr(0), bufSize, 0, 0
UTF16ToMultiByte = arr
End FunctionPrivate Sub Command1_Click()
MsgBox MultiByteToUTF16(UTF16ToMultiByte("ab中,c", cpUTF8), cpUTF8)
End Sub