vba 在 Excel 函数中将十六进制字符串转换为 base64

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23348212/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 02:54:50  来源:igfitidea点击:

Convert a hex string to base64 in an Excel function

excelvbabase64hex

提问by WickedMongoose

I have a lengthy string of hex values to convert to base64.

我有一个很长的十六进制值字符串要转换为 base64。

I'm looking for a simple format cell function such as =Hex2b64(Hexstring)that will accept any length of hex characters.

我正在寻找一个简单的格式单元格函数,例如=Hex2b64(Hexstring)可以接受任何长度的十六进制字符。

I have been using http://home.paulschou.net/tools/xlate/to do my conversion manually. The conversion works and the data is received by all relevant databases and parsed appropriately.

我一直在使用http://home.paulschou.net/tools/xlate/手动进行转换。转换有效,所有相关数据库接收数据并进行适当解析。

The data I am receiving is hex represented binary, which has been converted in multiple blocks and concatenated into long hex strings in accordance with project documentation that I am not privy to.

我收到的数据是十六进制表示的二进制文件,根据我不知道的项目文档,它已被转换为多个块并连接成长的十六进制字符串。

A typical Input String would be:

典型的输入字符串是:

Hex= 00014088F6650101393939393939392D30304646463238313030000343332353430342D35353FA10000002805900100002805

and the corresponding output would be:

相应的输出将是:

B64 = AAFAiPZlAQE5OTk5OTk5LTAwRkZGMjgxMDAAA0MzI1NDA0LTU1P6EAAAAoBZABAAAoAF

回答by omegastripes

Function Hex2Base64(ByVal sHex)

    Static oNode As Object
    Dim a() As Byte

    If Len(sHex) Mod 2 <> 0 Then
        sHex = Left(sHex, Len(sHex) - 1) & "0" & Right(sHex, 1)
    End If
    If oNode Is Nothing Then
        Set oNode = CreateObject("MSXML2.DOMDocument").createElement("Node")
    End If
    With oNode
        .text = ""
        .dataType = "bin.hex"
        .text = sHex
        a = .nodeTypedValue
        .dataType = "bin.base64"
        .nodeTypedValue = a
        Hex2Base64 = .text
    End With

End Function