VB中UTF8轉Unicode編碼

 
  1. Private Declare Function MultiByteToWideChar Lib "kernel32" (ByVal CodePage As LongByVal dwFlags As LongByRef lpMultiByteStr As Any, ByVal cchMultiByte As LongByVal lpWideCharStr As LongByVal cchWideChar As LongAs Long 
  2. Private Const CP_UTF8 = 65001  
  3.  
  4. 'Purpose:Convert Utf8 to Unicode  
  5. Public Function UTF8_Decode(ByVal sUTF8 As StringAs String 
  6.    Dim lngUtf8Size      As Long 
  7.    Dim strBuffer        As String 
  8.    Dim lngBufferSize    As Long 
  9.    Dim lngResult        As Long 
  10.    Dim bytUtf8()        As Byte 
  11.    Dim n                As Long 
  12.    If LenB(sUTF8) = 0 Then Exit Function 
  13.       On Error GoTo EndFunction  
  14.       bytUtf8 = StrConv(sUTF8, vbFromUnicode)  
  15.       lngUtf8Size = UBound(bytUtf8) + 1  
  16.       On Error GoTo 0  
  17.       lngBufferSize = lngUtf8Size * 2  
  18.       strBuffer = String$(lngBufferSize, vbNullChar)  
  19.       'Translate using code page 65001(UTF-8)  
  20.       lngResult = MultiByteToWideChar(CP_UTF8, 0, bytUtf8(0), _  
  21.          lngUtf8Size, StrPtr(strBuffer), lngBufferSize)  
  22.       'Trim result to actual length  
  23.       If lngResult Then 
  24.          UTF8_Decode = Left$(strBuffer, lngResult)  
  25.       End If 
  26. End Function 

 

把你上面獲取的內容轉一下就行,比如Text1.Text=UTF8_Decode(strResponse)
還可以加多一句InStr(1, strResponse, "charset=utf-8") > 0判斷是否是UTF8編碼再執行,不是UTF8的就直接顯示。 還有些網頁編碼那裏是大寫的,要這樣寫InStr(1, strResponse, "charset=UTF-8") > 0

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章