设为首页收藏本站Access中国

Office中国论坛/Access中国论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

返回列表 发新帖
查看: 4753|回复: 2
打印 上一主题 下一主题

[模块/函数] 二进制UTF8l转成字符串代码

[复制链接]

点击这里给我发消息

跳转到指定楼层
1#
发表于 2013-7-21 14:39:43 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
最近在处理二进制UTF8编码,分享一个函数

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

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

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏1 分享分享 分享淘帖 订阅订阅
2#
发表于 2014-5-13 22:34:13 | 只看该作者
好好学习,天天向上

点击这里给我发消息

3#
发表于 2014-8-16 05:32:57 来自手机 | 只看该作者
不错,用ADO,stream也可以吧?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|站长邮箱|小黑屋|手机版|Office中国/Access中国 ( 粤ICP备10043721号-1 )  

GMT+8, 2024-5-3 11:54 , Processed in 0.108875 second(s), 30 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表