integer to text binary, hex, or any other base (1 Viewer)

Status
Not open for further replies.

ozinm

Human Coffee Siphon
Local time
Today, 23:30
Joined
Jul 10, 2003
Messages
121
Hi all,
Had to write a quick and nasty function that outputted a number in base 36 (don't ask!) and thought I'd share the code.

You can modify this function to work out any base by removing the unwanted digits from the DigitArray.

E.G.
for Hex
DigitArray = Array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9" , "A", "B", "C", "D", "E", "F")
for Binary
DigitArray = Array("0", "1")

NB There's no overflow checking on this function yet!


Code:
Public Function EncodeNumber(INumber As Long) As String
Dim OutputString As String
Dim DigitArray
Dim TempNumber As Long
Dim TempColumn As Long
Dim TempDigit As Long
Dim RangeStart As Long
Dim RangeSize As Long
DigitArray = Array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9" _
, "A", "B", "C", "D", "E", "F" _
, "G", "H", "I", "J", "K", "L", "M", "N" _
, "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z")

OutputString = ""
TempColumn = 1
TempNumber = INumber
RangeStart = LBound(DigitArray)
RangeSize = (UBound(DigitArray) - RangeStart) + 1

While TempNumber > 0
    TempDigit = TempNumber - (Int(TempNumber / (TempColumn * RangeSize)) * (TempColumn * RangeSize))
    TempNumber = TempNumber - TempDigit
    OutputString = DigitArray((TempDigit / TempColumn) + RangeStart) & OutputString
    TempColumn = (TempColumn * RangeSize)
Wend

EncodeNumber = OutputString

End Function
 
Status
Not open for further replies.

Users who are viewing this thread

Top Bottom