The contact management data was going to create and maintain customer accounts in the financial package. The problem was in the access DB each contact had a unique number and in the financial package we needed a reference where the first 2 letters of the customers surname were followed with up to 4 more characters for a unique reference.
Tricky.
In the end I put together this short bit of code that allows you to convert a number to any base. All you have to do is supply the digits to use.
In my case I've used base 33 to pack the id numbers into a shorter format using numbers and letters.
Const vbB2Digits = "01" 'base 2 - Binary
Const vbB8Digits = "01234567" 'base 8 - Octal
Const vbB10Digits = "0123456789" 'base 10 - Decimal
Const vbB10Digits = "0123456789XE" 'base 12 - Dozenal. See http://www.dozenal.org
Const vbB16Digits = "0123456789ABCDEF" 'base 16 - Hexedecimal
Const vbB33Digits = "0123456789ABCDEFGHJKMNPQRSTUVWXYZ" 'base 33 - non similar characters (I,L,O removed) from 0-9 & Alphabet
Public Function sEncodeNumber(lNumber As Long, Optional sDigitList As String = vbB10Digits) As String
Dim sOutputString As String
Dim lTmpNumber As Long
Dim lRangeSize As Long
Dim lTmpPower As Long
Dim lTmpLastPower As Long
OutputString = ""
lTmpNumber = lNumber
lRangeSize = Len(sDigitList)
lTmpPower = 1
' Work with postive numbers
If lTmpNumber < 0 Then
lTmpNumber = lTmpNumber * -1
End If
While lTmpNumber > 0
lTmpLastPower = lTmpPower
lTmpPower = lTmpPower * lRangeSize
sOutputString = Mid(sDigitList, ((lTmpNumber Mod lTmpPower) / lTmpLastPower) + 1, 1) & sOutputString
lTmpNumber = lTmpNumber - (lTmpNumber Mod lTmpPower)
Wend
' insert the -ve sign if the orginal number was negative
If lNumber < 0 Then
sOutputString = "-" & sOutputString
End If
sEncodeNumber = sOutputString
End Function
For those maths geeks out there, if you haven't come across dozenal before, have a quick gander at Alex Bellos' blog entires on the subject.





0 comments:
Post a Comment