Search code examples
vbscriptasp-classic

Classic ASP/VBScript implementation of Crockford's Base32 Encoding


Moving on from the first part of my problem, I'm now working on trying to write a Classic ASP/VBScript implementation of Douglas Crockford's Base32 Encoding.

Time is tight on this particular project, so while I am working this out on my own, I'm hoping someone has something readily at hand (or can at least whip one up faster than I can).


Solution

  • First run... does this look about right?

    'Base32 encoding functions for shorter, less confusing verification numbers'
    Const kBase32Digits = "0123456789abcdefghjkmnpqrstvwxyz"
    'To Base32'
    Function ToBase32(ByVal lInput)
        Dim lModulo, sTemp
        Do Until lInput = 0
            lModulo = lInput Mod 32
            sTemp = Mid(kBase32Digits, lModulo + 1, 1) & sTemp
            lInput = lInput \ 32
        Loop
        ToBase32 = sTemp
    End Function
    'From Base32'
    Function FromBase32(ByVal sInput)
        Dim sTemp, sR, i,iY,lLen, zMultiplier
        sTemp = LCase(sInput)
        sTemp = Replace(sTemp,"o","0")
        sTemp = Replace(sTemp,"i","1")
        sTemp = Replace(sTemp,"l","1")
        sTemp = Replace(sTemp,"u","v")
        zMultiplier = 1
        lLen = Len(sTemp)
        For i = lLen To 1 Step -1
            sR = Mid(sTemp, i, 1)
            iY = InStr(1, kBase32Digits, sR, vbTextCompare) - 1
            FromBase32 = FromBase32 + iY * zMultiplier
            zMultiplier = zMultiplier * 32
        Next
    End Function
    

    Edit: Seems to work fine so far... I'll go with this unless someone posts something better.