Convert a hex string to base64 in an excel function

匿名 (未验证) 提交于 2019-12-03 02:31:01

问题:

I have a lengthy string of hex values that need to be converted to base64.

I'm looking for a simple format cell function such as =Hex2b64(Hexstring) that will accept any length of hex characters. I have found pieces here and there but no one simple code block.

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

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

回答1:

Function Hex2Base64(byVal strHex)     Dim arrBytes     If Len(strHex) Mod 2 <> 0 Then         strHex = Left(strHex, Len(strHex) - 1) & "0" & Right(strHex, 1)     End If     With CreateObject("Microsoft.XMLDOM").createElement("objNode")         .DataType = "bin.hex"         .Text = strHex         arrBytes = .nodeTypedValue     End With     With CreateObject("Microsoft.XMLDOM").createElement("objNode")         .DataType = "bin.base64"         .nodeTypedValue = arrBytes         Hex2Base64 = .Text     End With End Function 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!