当前位置:编程学习 > VB >>

VB编程中如何将UTF-8和Unicode互相转换?

本人相菜鸟,希望注释下哦,还有用法。

 

VB编程中如何将UTF-8编码和Unicode编码互相转换?把UTF-8转码成Unicode的,和把UTF-8转成Unicode.....

追问:如果我想把wap网页的源码转成unicode格式的呢?我用这个方法获取的源码:

Private Declare Function InternetOpen Lib "Wininet.dll" Alias "InternetOpenA" (ByVal sAgent As String, ByVal lAccessType As Long, ByVal sProxyName As String, ByVal sProxyBypass As String, ByVal lFlags As Long) As Long
Private Declare Function InternetOpenUrl Lib "Wininet.dll" Alias "InternetOpenUrlA" (ByVal hInternetSession As Long, ByVal lpszUrl As String, ByVal lpszHeaders As String, ByVal dwHeadersLength As Long, ByVal dwFlags As Long, ByVal dwContext As Long) As Long
Private Declare Function InternetReadFile Lib "Wininet.dll" (ByVal hFile As Long, sBuffer As Any, ByVal lNumBytesToRead As Long, lNumberOfBytesRead As Long) As Integer
Private Declare Function InternetCloseHandle Lib "Wininet.dll" (ByVal hInet As Long) As Integer
Private Const OnceLen = 2048
Private Const INTERNET_OPEN_TYPE_DIRECT = 1
Private Const INTERNET_FLAG_RELOAD = &H80000000
Dim sq As String

Dim p As String
'打开URL函数
Function OpenURL(ByVal sURL As String) As String
    Dim hOpen As Long, hFile As Long, RetLen As Long, Buffer() As Byte, szBuffer As String
    hOpen = InternetOpen(vbNullString, INTERNET_OPEN_TYPE_DIRECT, vbNullString, vbNullString, 0)
    hFile = InternetOpenUrl(hOpen, sURL, vbNullString, ByVal 0&, INTERNET_FLAG_RELOAD, ByVal 0&)
    If hFile <> 0 Then
        Do
            ReDim Buffer(OnceLen - 1)
            InternetReadFile hFile, ByVal VarPtr(Buffer(0)), OnceLen, RetLen
            DoEvents
            If RetLen = 0 Then Exit Do
            If RetLen < OnceLen Then ReDim Preserve Buffer(RetLen - 1)
            szBuffer = szBuffer & CStr(Buffer)
        Loop
        InternetCloseHandle hFile
    End If
    InternetCloseHandle hOpen
    OpenURL = StrConv(szBuffer, vbUnicode)
End Function


Private Sub Form_Load()
p= OpenURL(" http://wap.zhaoxi.net")

byt = Utf8ToUnicode(p)'这地方老是出问题,该怎么弄?就这个问题
End Sub

答案: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 Long
Private Const CP_UTF8 = 65001


‘utf8转unicode
Function Utf8ToUnicode(ByRef Utf() As Byte) As String
    Dim lRet As Long
    Dim lLength As Long
    Dim lBufferSize As Long
    lLength = UBound(Utf) - LBound(Utf) + 1
    If lLength <= 0 Then Exit Function
    lBufferSize = lLength * 2
    Utf8ToUnicode = String$(lBufferSize, Chr(0))
    lRet = MultiByteToWideChar(CP_UTF8, 0, VarPtr(Utf(0)), lLength, StrPtr(Utf8ToUnicode), lBufferSize)
    If lRet <> 0 Then
        Utf8ToUnicode = Left(Utf8ToUnicode, lRet)
    End If
End Function

‘unicode转utf8
Function UnicodeToUtf8(ByVal UCS As String) As Byte()
    Dim lLength As Long
    Dim lBufferSize As Long
    Dim lResult As Long
    Dim abUTF8() As Byte
    lLength = Len(UCS)
    If lLength = 0 Then Exit Function
    lBufferSize = lLength * 3 + 1
    ReDim abUTF8(lBufferSize - 1)
    lResult = WideCharToMultiByte(CP_UTF8, 0, StrPtr(UCS), lLength, abUTF8(0), lBufferSize, vbNullString, 0)
    If lResult <> 0 Then
    lResult = lResult - 1
    ReDim Preserve abUTF8(lResult)
    UnicodeToUtf8 = abUTF8
    End If
End Function

Private Sub Command1_Click()
    Dim byt() As Byte
    byt = UnicodeToUtf8("测试")
    Debug.Print Hex(byt(0)) & Hex(byt(1)) & Hex(byt(2))
    Debug.Print Utf8ToUnicode(byt())   
End Sub

 

上一个:请问,学vB编程需要从那里开学?谢谢
下一个:VB编程高手进来下...

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,