Visual Basic Numbering Functions
Submitted By:
Nad__Af
Rating:





(
Rate It)
Visual Basic Numbering Systems Functions
ToRadix and ToRadix2 update
This file contains functions useful for converting numbers from decimal to a number in a different numbering system such as binary, hexadecimal, octal and all others. For example, you can convert a decimal number to any number in any radix you like, the radix could be octal, hexadecimal, binary or any. You just assign the radix instead of the value xRad:
ToRadix(1200, 2) // gives 10010110000
ToRadix(1200, 8) // gives 2260
ToRadix(1200, 5) // gives 14300
ToRadix2(1200, 16) // gives 4B0
ToRadix2(1200, 20) // gives 300
you can convert even this:
ToRadix( 9999999999999999999999999999, 2) '28-digit
ToRadix2( 9999999999999999999999999999, 15) '28-digit
We used ToRadix2 because all the values over ten are assigned manually in the function body.
==================================================================================
' the following function converts any decimal number to the number in radix
' xRad; however, the radix xRad has to be no greater than 10, if you want
' the radix xRad to be greater than 10, see ToRadix2
'by: Nadeem Afanah
'e-mail: [[Email Removed]]
Function ToRadix(ByVal dNum As Decimal, ByVal xRad As Byte) As String
Dim length As Decimal
Dim temp As Decimal
Dim sRes As String
Dim I As Long
I = Math.Log10(10)
length = (Math.Log10(dNum) / Math.Log10(xRad)) + 1
If length <> Int(length) Then length = CLng(length) + 1
For I = 0 To length - 1
sRes = sRes & Int(dNum / (pow(xRad, (length - I - 1))))
temp = (pow(xRad, (length - I - 1)))
dNum = dNum - (Int(dNum / temp) * temp)
Next
'the following function removes any leading zeros (if any)
'you can enable the following lines of code, but i might slow down your function
'Dim iC As Long
'Do
'If Left(sRes, 1) = "0" Then
'sRes = Mid$(sRes, 2)
'Else
'Exit Do
'End If
'Loop
ToRadix = sRes
End Function
'=================================================================================
'The 2nd version of ToRadix converts any number to any radix, because here we've
'assigned the values A, B, C, ... up to twenty ourself; however, this is not
'limited to twenty you can continue assigning as many values as you wish
'NOTE: value returned from function for radix over 10 are always BIG-ENDIANed.
'by: Nadeem Afanah
'e-mail: [[Email Removed]]
Function ToRadix2(ByVal dNum As Decimal, ByVal xRad As Byte) As String
Dim length As Decimal
Dim sRes As String
Dim sVal As String
Dim temp As Decimal
Dim I As Long
length = (Math.Log10(dNum) / Math.Log10(xRad)) + 1
If length <> Int(length) Then length = Int(length) + 1
For I = 0 To length - 1
temp = Int(dNum / (pow(xRad, (length - I - 1))))
Select Case temp
Case Is = 10
sVal = "A"
Case Is = 11
sVal = "B"
Case Is = 12
sVal = "C"
Case Is = 13
sVal = "D"
Case Is = 14
sVal = "E"
Case Is = 15
sVal = "F"
Case Is = 16
sVal = "G"
Case Is = 17
sVal = "H"
Case Is = 18
sVal = "I"
Case Is = 19
sVal = "J"
Case Is = 20
sVal = "K"
Case Else
sVal = Int(dNum / (pow(xRad, (length - I - 1))))
End Select
sRes = sRes & sVal
temp = pow(xRad, (length - I - 1))
dNum = dNum - (Int(dNum / temp) * temp)
Next
'the following function removes any leading zeros (if any)
'you can enable the following lines of code, but i might slow down your function
'Dim iC As Long
'Do
'If Left(sRes, 1) = "0" Then
'sRes = Mid$(sRes, 2)
'Else
'Exit Do
'End If
'Loop
ToRadix2 = sRes
End Function
'=========================================================================================
'The following function is needed to calculate powers of numbers.
Function pow(ByVal Base As Long, ByVal Num As Long) As Decimal
Dim I As Long
pow = Base
If Num = 0 Then
pow = 1
Exit Function
End If
For I = 1 To Num - 1
pow = pow * Base
Next
End Function