Convert hex to base58 to excel function

This question is related to this post: Convert hex string to base64 to excel function

My question is, do we have a similar function for base58?

I am trying to write a function using directions in this tutorial

https://www.youtube.com/watch?v=vw7BhqsjYFk

This is my attempt.

Sub Test()
    Debug.Print Hex2Base58("801ba5dbc682d1ba3a37314b1ceff1431735b9ee16a447086d0568df9d3de07417e819f299")
End Sub


Function Hex2Base58(strHex As String) As String

Dim strLen As Long, i As Long, j As Long
Dim ascVal()
Dim asc2Bin()
Dim joinedBin As String
Dim bin2DecVal()
Dim base58Eq()

strLen = Len(strHex)
If strLen = 0 Then Hex2Base58 = "No Input": Exit Function

If strLen Mod 3 <> 0 Then
    For i = 1 To 4
        If strLen Mod 3 = 0 Then Exit For
        strHex = strHex & Chr(0)
        strLen = strLen + 1
    Next i
End If


For i = 1 To strLen

    'Ascii value
    ReDim Preserve ascVal(i - 1)
    ascVal(i - 1) = Asc(CStr(Mid(strHex, i, 1)))

    'Ascii to 8 bit binary
    ReDim Preserve asc2Bin(i - 1)
    asc2Bin(i - 1) = WorksheetFunction.Dec2Bin(ascVal(i - 1), 8)

Next i

'Join Binary then create groups of 6 bits
joinedBin = Join(asc2Bin, "")

j = 0
For i = 1 To Len(joinedBin) Step 6
    'Decimal equivalent of 6 bit binary group
    ReDim Preserve bin2DecVal(j)
    bin2DecVal(j) = WorksheetFunction.Bin2Dec(Mid(joinedBin, i, 6))

    ReDim Preserve base58Eq(j)
    base58Eq(j) = lookupBase58Eq(CStr(bin2DecVal(j)))

    j = j + 1
Next i

Hex2Base58 = Join(base58Eq, "")

End Function


Function lookupBase58Eq(decVal As String) As String
Dim aCell As Range

With b58

Set aCell = .Range("A1:A" & .Range("A" & .Rows.Count).End(xlUp).Row).Find(What:=decVal, LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=True)
If Not aCell Is Nothing Then
    lookupBase58Eq = .Range("B" & aCell.Row)
End If

End With

End Function

      

When testing it seems like every next step matches the tutorial, but when I converted "Hello" to base58 I got the result as "K7d1" which is good according to this lookup table https://en.bitcoin.it/wiki/Base58Check_encoding ... But on this site https://www.browserling.com/tools/base58-encode it evaluates to "6Wc", not sure what the difference is.

+3


source to share





All Articles