|
|
|
Click here to copy the following block |
Option Explicit
Private Const ERROR_NUMBER = 13&
Private Const HexChars = "0123456789ABCDEF" Private Const AlphaChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
Public Enum Bases ebBinary = 2& ebOctal = 8& ebDecimal = 10& ebHexadecimal = 16& ebAlphabet = 26& ebSexagesimal = 60& End Enum
Private Function DigitLength(Base As Bases) As Long
Select Case Base Case ebSexagesimal: DigitLength = 3 Case Else DigitLength = 1 End Select End Function
Private Function Floor(ByVal Number As Double) As Double
If Int(Number) > Number Then Floor = Int(Number) - 1 Else Floor = Int(Number) End If End Function
Private Function GetNumDec(dblTemp As Double, PadTo As Long, _ Base As Bases) As Long
Dim lTemp As Long, lTempPad As Double If dblTemp = 0 Then lTemp = 1 Else lTemp = Floor(Log(dblTemp) / Log(Base)) + 1 End If If PadTo > 1 Then lTempPad = lTemp / CDbl(PadTo) If lTempPad > Floor(lTempPad) Then lTempPad = 1 + Floor(lTempPad) - lTempPad lTempPad = lTempPad * PadTo lTemp = lTemp + lTempPad End If End If GetNumDec = lTemp End Function
Private Function ConvertDigit(lngDigit As Long, Base As Bases) As String
If lngDigit >= Base Then Err.Raise ERROR_NUMBER, "ConvertDigit", "Invalid digit for base" Else Select Case Base Case ebBinary, ebOctal, ebDecimal: ConvertDigit = CStr(lngDigit) Case ebHexadecimal: ConvertDigit = Mid$(HexChars, lngDigit + 1, 1) Case ebAlphabet: ConvertDigit = Mid$(AlphaChars, lngDigit + 1, 1) Case ebSexagesimal: ConvertDigit = Right$("00" & CStr(lngDigit), 2) & ":" Case Else: Err.Raise ERROR_NUMBER, "ConvertDigit", "Unknown base" End Select End If End Function
Private Function DeconvertDigit(strDigit As String, Base As Bases) As Long
Dim lngTemp As Long Select Case Base Case ebBinary, ebOctal, ebDecimal: If IsNumeric(strDigit) Then lngTemp = CLng(strDigit) If lngTemp < Base Then DeconvertDigit = lngTemp Else Err.Raise ERROR_NUMBER, "DeconvertDigit", _ "Invalid digit for base" End If Else Err.Raise ERROR_NUMBER, "DeconvertDigit", "Invalid character" End If Case ebHexadecimal: lngTemp = InStr(1, HexChars, UCase$(strDigit)) If lngTemp = 0 Then Err.Raise ERROR_NUMBER, "DeconvertDigit", _ "Invalid digit for base" Else DeconvertDigit = lngTemp - 1 End If Case ebAlphabet: lngTemp = InStr(1, AlphaChars, UCase$(strDigit)) If lngTemp = 0 Then Err.Raise ERROR_NUMBER, "DeconvertDigit", _ "Invalid Alpha Character" Else DeconvertDigit = lngTemp - 1 End If Case ebSexagesimal: If Len(strDigit) = 3 Then If Right$(strDigit, 1) = ":" And IsNumeric(Left$(strDigit, _ 2)) Then lngTemp = CLng(Left$(strDigit, 2)) If lngTemp < Base Then DeconvertDigit = lngTemp Else Err.Raise ERROR_NUMBER, "DeconvertDigit", _ "Invalid digit for base" End If Else Err.Raise ERROR_NUMBER, "DeconvertDigit", _ "Invalid digit for base" End If Else Err.Raise ERROR_NUMBER, "DeconvertDigit", _ "Invalid digit for base" End If Case Else: Err.Raise ERROR_NUMBER, "DeconvertDigit", "Unknown base" End Select End Function
Private Function ConvertDec2Base(ByVal Number, ByVal Base As Bases, _ Optional NumDecimals As Long = -1, Optional Tolerance As Double = 1E-27, _ Optional PadTo As Long = 0) As String
Dim dblTemp As Double Dim lCDec As Long Dim lDigit As Long Dim dblPwr As Double Dim strTemp As String
If Not IsNumeric(Number) Then Err.Raise ERROR_NUMBER, "ConvertDec2Base", "Number must be decimal" ElseIf Base < 2 Then Err.Raise ERROR_NUMBER, "ConvertDec2Base", "Invalid base" Else Tolerance = Abs(Tolerance) dblTemp = CDbl(Number) If dblTemp < 0 Then strTemp = "-" dblTemp = -dblTemp End If lCDec = GetNumDec(dblTemp, PadTo, Base) If lCDec = 0 Then strTemp = strTemp & "0" Else Do Until lCDec = 0 lCDec = lCDec - 1 dblPwr = Base ^ lCDec lDigit = 0 Do While dblTemp >= dblPwr lDigit = lDigit + 1 dblTemp = dblTemp - dblPwr Loop strTemp = strTemp & ConvertDigit(lDigit, Base) Loop End If If dblTemp > Tolerance And (NumDecimals > 0 Or (NumDecimals = -1 And _ Tolerance > 0)) Then strTemp = strTemp & "." Do While dblTemp > Tolerance And (lCDec > (-NumDecimals) Or _ NumDecimals = -1) lCDec = lCDec - 1 dblPwr = Base ^ lCDec lDigit = 0 Do While dblTemp >= dblPwr lDigit = lDigit + 1 dblTemp = dblTemp - dblPwr Loop strTemp = strTemp & ConvertDigit(lDigit, Base) Loop End If ConvertDec2Base = strTemp End If End Function
Private Function ConvertBase2Dec(ByVal Number As String, _ ByVal Base As Bases) As Double
Dim dblTemp As Double Dim strDigit As String, lngDigit As Long, i As Long Dim lngPwr As Long, lngSign As Long, lngDigitSize If Base < 2 Then Err.Raise ERROR_NUMBER, "ConvertBase2Dec", "Invalid Base" Else lngDigitSize = DigitLength(Base) lngPwr = 0 lngSign = 1 i = 1 Do Until i > Len(Number) strDigit = Mid$(Number, i, lngDigitSize) If Left$(strDigit, 1) = "." Then i = i + 1 If lngPwr = 0 Then lngPwr = 1 Else Err.Raise ERROR_NUMBER, "ConvertBase2Dec", _ "More than one decimal point" End If ElseIf Left$(strDigit, 1) = "-" Then i = i + 1 If lngPwr = 0 And dblTemp = 0 Then lngSign = -lngSign Else Err.Raise ERROR_NUMBER, "ConvertBase2Dec", _ "Invalid negation" End If Else i = i + lngDigitSize lngDigit = DeconvertDigit(strDigit, Base) dblTemp = dblTemp * Base + lngDigit lngPwr = lngPwr * Base End If Loop If lngPwr > 1 Then ConvertBase2Dec = CDbl(lngSign) * (dblTemp / CDbl(lngPwr)) Else ConvertBase2Dec = CDbl(lngSign) * dblTemp End If End If End Function
Public Function ConvertBase(ByVal Number, ByVal FromBASE As Bases, _ Optional ByVal ToBASE As Bases = ebDecimal, Optional NumDecimals As Long = - _ 1, Optional Tolerance As Double = 1E-27, Optional PadTo As Long = 0) As _ Variant
Dim dblDec As Double If FromBASE = ebDecimal Then If IsNumeric(Number) Then dblDec = CDbl(Number) Else Err.Raise ERROR_NUMBER, "ConvertBase", "Not a decimal number" End If Else dblDec = ConvertBase2Dec(CStr(Number), FromBASE) End If If ToBASE = ebDecimal Then ConvertBase = dblDec Else ConvertBase = ConvertDec2Base(dblDec, ToBASE, NumDecimals, Tolerance, _ PadTo) End If End Function
Public Function LogB(ByVal dblNumber As Double, ByVal lBase As Long) As Double LogB = Log(dblNumber) / Log(lBase) End Function |
|
|
|
Submitted By :
Nayan Patel
(Member Since : 5/26/2004 12:23:06 PM)
|
|
|
Job Description :
He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting. |
View all (893) submissions by this author
(Birth Date : 7/14/1981 ) |
|
|