Home > Software engineering >  StrConv problem
StrConv problem

Time:09-30

StrConv (* * * * *, vbFromUnicode)

Here UTF8 appear odd into transcoding?? But even normal transcoding ~ could you tell me how to solve, should or is there a way to replace,

CodePudding user response:

StrConv does not support the UTF8,
Why turn unicode utf incomplete in the code # 19

CodePudding user response:

In turn UTF8 yourself or use API calls vbcorlib ready-made

CodePudding user response:

 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
'the commonly used code page:
Const cpUTF8=65001
Const cpGB2312=936
Const cpGB18030=54936
Const cpUTF7=65000
The Function MultiByteToUTF16 (UTF8 () As Byte, CodePage As Long) As String
Dim bufSize As Long
BufSize=MultiByteToWideChar (CodePage, 0 & amp; , UTF8 (0), UBound (UTF8) + 1, 0, 0)
MultiByteToUTF16=Space (bufSize)
MultiByteToWideChar CodePage, 0 & amp; , UTF8 (0), UBound (UTF8) + 1, StrPtr (MultiByteToUTF16), bufSize
End the Function

The Function UTF16ToMultiByte (UTF16 As String, CodePage As Long) As Byte ()
Dim bufSize As Long
Dim arr () As Byte
BufSize=WideCharToMultiByte (CodePage, 0 & amp; , StrPtr (UTF16), Len (UTF16), 0, 0, 0, 0)
ReDim arr (bufSize - 1)
WideCharToMultiByte CodePage, 0 & amp; , StrPtr (UTF16), Len (UTF16), arr (0), bufSize, 0, 0
UTF16ToMultiByte=arr
End the Function

Private Sub Command1_Click ()
MsgBox MultiByteToUTF16 (UTF16ToMultiByte (" ab, c ", cpUTF8), cpUTF8)
End Sub

CodePudding user response:

 
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 the VB a string, the fill a byte array with matching utf-8 data. Returns TRUE if successful; FALSE otherwise
The 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

The 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.
The Else

'Prep a temporary byte buffer
ReDim dstUtf8 As Byte (0 To lenUTF8-1)

'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 the when calculating a buffer length, only to
'fail here, but better safe than sorry.)
If lenUTF8 & lt;> 0 Then
StringToUTF8Bytes=True
The Else
The 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 the If

End the If

End the Function

'Given a byte array containing the utf-8 data, return the data as a VB string.
The 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

nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related