如何将二进制UTF8转成字符串

分享一个utf-8转字符串的函数。解决编码带来的错误!
调用方法: 

Private Sub cmdConvert_Click()
    fm20TxtText.Text = FromUtf8(FromHex(fm20TxtHex.Text))
End Sub

函数如下:
Private Declare Function CryptStringToBinary Lib "Crypt32" _

    Alias "CryptStringToBinaryW" ( _
    ByVal pszString As Long, _
    ByVal cchString As Long, _
    ByVal dwFlags As Long, _
    ByVal pbBinary As Long, _
    ByRef pcbBinary As Long, _
    ByRef pdwSkip As Long, _
    ByRef pdwFlags 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

Public Function FromHex(ByRef HexString As String) As Byte()
    Const CRYPT_STRING_HEX As Long = &H4&
    Dim lngOutLen As Long
    Dim dwActualUsed As Long
    Dim bytBinary() As Byte

    If Len(HexString) < 1 Then Exit Function

    'Determine output buffer length required.
    If CryptStringToBinary(StrPtr(HexString), _
                           Len(HexString), _
                           CRYPT_STRING_HEX, _
                           0&, _
                           lngOutLen, _
                           ByVal 0&, _
                           dwActualUsed) = 0 Then
        Err.Raise &H80044100, "FromHex", _
                  "CryptStringToBinary failed, error " & CStr(Err.LastDllError)
    Else
        'Convert to binary.
        ReDim bytBinary(lngOutLen - 1)
        If CryptStringToBinary(StrPtr(HexString), _
                               Len(HexString), _
                               CRYPT_STRING_HEX, _
                               VarPtr(bytBinary(0)), _
                               lngOutLen, _

                               ByVal 0&, _
                               dwActualUsed) = 0 Then
            Err.Raise &H80044100, "FromHex", _
                      "CryptStringToBinary failed, error " & CStr(Err.LastDllError)
        Else
            FromHex = bytBinary
        End If
    End If
End Function

Public Function FromUtf8(ByRef Utf8() As Byte) As String
    Const CP_UTF8 As Long = 65001
    Dim lngBytes As Long
    Dim lngResult As Long

    On Error Resume Next
    lngBytes = UBound(Utf8) - LBound(Utf8) + 1
    If Err Then
        Err.Clear
        On Error GoTo 0
        Err.Raise 5, "FromUtf8", "Invalid parameter: must be a dimensioned array"
    End If
    On Error GoTo 0
    lngResult = MultiByteToWideChar(CP_UTF8, 0, VarPtr(Utf8(LBound(Utf8))), _
                                    lngBytes, 0, 0)
    FromUtf8 = String$(lngResult, 0)
    MultiByteToWideChar CP_UTF8, 0, VarPtr(Utf8(LBound(Utf8))), _
                        lngBytes, StrPtr(FromUtf8), lngResult
End Function

 

扩展阅读:

用vba把文本文件UTF-8 编码转换为 ANSI​   答案在5楼

转载于:https://www.cnblogs.com/officecn/p/3205740.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值