StrConv(*****, vbFromUnicode)
這里UTF8轉碼的時候會出現 奇數 變成??。而偶數正常轉碼的情況~ 請問應該怎么解決。或者有沒有替換的方法。
uj5u.com熱心網友回復:
StrConv 不支持 UTF8。unicode轉utf不完整 為什么啊 奉上代碼 #19
uj5u.com熱心網友回復:
要轉UTF8自己使用API或呼叫vbcorlib里現成的就是了uj5u.com熱心網友回復:
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 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 Function
Function 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 Function
Private Sub Command1_Click()
MsgBox MultiByteToUTF16(UTF16ToMultiByte("ab中,c", cpUTF8), cpUTF8)
End Sub
uj5u.com熱心網友回復:
Option Explicit
'Helper APIs for dealing with various charset conversions.
Private Const CP_UTF8 As Long = 65001
Private Declare Function MultiByteToWideChar Lib "kernel32" (ByVal CodePage As Long, ByVal dwFlags As Long, ByVal lpMultiByteStr As Long, ByVal cbMultiByte 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
'Given a VB string, fill a byte array with matching UTF-8 data. Returns TRUE if successful; FALSE otherwise
Public Function StringToUTF8Bytes(ByRef srcString As String, ByRef dstUtf8() As Byte) As Boolean
'Use WideCharToMultiByte() to calculate the required size of the final UTF-8 array.
Dim lenUTF8 As Long
lenUTF8 = WideCharToMultiByte(CP_UTF8, 0, StrPtr(srcString), Len(srcString), 0, 0, 0, 0)
'If the returned length is 0, WideCharToMultiByte failed. This typically only happens if totally invalid character combinations are found.
If lenUTF8 = 0 Then
Debug.Print "StringToUTF8Bytes() failed because WideCharToMultiByte did not return a valid buffer length.)"
Err.Raise Err.LastDllError, "StringToUTF8Bytes", "WideCharToMultiByte"
StringToUTF8Bytes = False
'The returned length is non-zero. Prep a buffer, then process the bytes.
Else
'Prep a temporary byte buffer
ReDim dstUtf8(0 To lenUTF8 - 1) As Byte
'Use the API to perform the actual conversion
lenUTF8 = WideCharToMultiByte(CP_UTF8, 0, StrPtr(srcString), Len(srcString), VarPtr(dstUtf8(0)), lenUTF8, 0, 0)
'Make sure the conversion was successful. (There is generally no reason for it to succeed when calculating a buffer length, only to
' fail here, but better safe than sorry.)
If lenUTF8 <> 0 Then
StringToUTF8Bytes = True
Else
Debug.Print "StringToUTF8Bytes() failed because WideCharToMultiByte could not perform the conversion, despite returning a valid buffer length.)"
Err.Raise Err.LastDllError, "StringToUTF8Bytes", "WideCharToMultiByte"
StringToUTF8Bytes = False
End If
End If
End Function
'Given a byte array containing UTF-8 data, return the data as a VB string.
Public Function UTF8BytesToString(ByRef Utf8() As Byte) As String
'Use MultiByteToWideChar() to calculate the required size of the final string (e.g. UTF-8 expanded to VB's default wide character set).
Dim lenWideString As Long
lenWideString = MultiByteToWideChar(CP_UTF8, 0, VarPtr(Utf8(0)), UBound(Utf8) + 1, 0, 0)
'If the returned length is 0, MultiByteToWideChar failed. This typically only happens if totally invalid characters are found.
If lenWideString = 0 Then
Debug.Print "UTF8BytesToString() failed because MultiByteToWideChar did not return a valid buffer length.)"
Err.Raise Err.LastDllError, "UTF8BytesToString", "MultiByteToWideChar"
UTF8BytesToString = ""
'The returned length is non-zero. Prep a buffer, then retrieve the bytes.
Else
'Prep a temporary string buffer
Dim strWide As String
strWide = String$(lenWideString, 0)
'Use the API to perform the actual conversion
lenWideString = MultiByteToWideChar(CP_UTF8, 0, VarPtr(Utf8(0)), UBound(Utf8) + 1, StrPtr(strWide), lenWideString)
'Make sure the conversion was successful. (There is generally no reason for it to succeed when calculating a buffer length, only to
' fail here, but better safe than sorry.)
If lenWideString = 0 Then
Debug.Print "UTF8BytesToString() failed because MultiByteToWideChar could not perform the conversion, despite returning a valid buffer length.)"
Err.Raise Err.LastDllError, "UTF8BytesToString", "MultiByteToWideChar"
UTF8BytesToString = ""
Else
UTF8BytesToString = strWide
End If
End If
End Function
uj5u.com熱心網友回復:
盡量不要用StrConv,我從來不用,因為它不是Unicode Friendly的函式。uj5u.com熱心網友回復:
樓上真逗,有用就行轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/99671.html
標籤:VB基礎類
上一篇:VB程式運行無回應
