udfConvertToBase
udfConvertFromBase
str udfConvertToBase (int, int, int)
int udfConvertFromBase (str, int)
#DefineFunction udfConvertToBase (intNumBase10, intToBase, intPadLeft)
strFunction = "udfConvertToBase (intNumBase10, intToBase, intPadLeft)"
Terminate (VarType (intNumBase10) != 1, strFunction, "Parameter ""intNumBase10"" must be integer.")
Terminate (VarType (intToBase) != 1, strFunction, "Parameter ""intToBase"" must be integer.")
Terminate (VarType (intPadLeft) != 1, strFunction, "Parameter ""intPadLeft"" must be integer.")
Terminate ((intToBase < 2) || (intToBase > 36), strFunction, "Parameter ""intToBase"" must be in range 2..36.")
strToBase = ""
While !!intNumBase10
   strToBase = StrSub ("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ", 1 + (intNumBase10 mod intToBase), 1) : strToBase
   intNumBase10 = intNumBase10 / intToBase
EndWhile
If strToBase == "" Then strToBase = "0"
If !!intPadLeft Then strToBase = StrFixLeft (strToBase, "0", intPadLeft)
Return strToBase
;..........................................................................................................................................
; Conf:  WinBatch
; From:  kdmoyers admin@guden.com
; Date:  Thursday, December 27, 2001 12:50 PM
;..........................................................................................................................................
; Modified by Detlev Dalitz.20020204.20100207.
;..........................................................................................................................................
#EndFunction



#DefineFunction udfConvertFromBase (strFromBase, intFromBase)
strFunction = "udfConvertFromBase (strFromBase, intFromBase)"
Terminate (VarType (strFromBase) != 2, strFunction, "Parameter ""strFromBase"" must be string.")
Terminate (VarType (intFromBase) != 1, strFunction, "Parameter ""intFromBase"" must be integer.")
Terminate ((intFromBase < 2) || (intFromBase > 36), strFunction, "Parameter ""intFromBase"" must be in range 2..36.")
intNumBase10 = 0
While strFromBase != ""
   intI = StrIndex ("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ", StrSub (strFromBase, 1, 1), 1, 0)
   If intI == 0 Then Return -1
   If intI > intFromBase Then Return -1
   intNumBase10 = intNumBase10 * intFromBase + intI - 1
   strFromBase = StrSub (strFromBase, 2, -1)
EndWhile
Return intNumBase10
;..........................................................................................................................................
; Conf:  WinBatch
; From:  kdmoyers admin@guden.com
; Date:  Thursday, December 27, 2001 12:50 PM
;..........................................................................................................................................
; Modified by Detlev Dalitz.20020204.20100207.
;..........................................................................................................................................
#EndFunction


; Test.
intNumBase10 = 100
strListTemp = ""

:Test1
strMsgToBase = "Convert Base 10 decimal number " : intNumBase10 : " to Base B (2..36) number:" : @CRLF
For intBase = 2 To 36
   strToBase = udfConvertToBase (intNumBase10, intBase, 8)
   strListTemp = ItemInsert (strToBase, -1, strListTemp, @TAB)
   strMsgToBase = strMsgToBase : @CRLF : "Base" : @TAB : intBase : @TAB : strToBase
Next
strMsgTitle = "Demo udfConvertToBase (intNumBase10, intToBase, intPadLeft)"
Message (strMsgTitle, strMsgToBase)


:Test2
strMsgFromBase = "Convert Base B (2..36) number to Base 10 decimal number:" : @CRLF
For intBase = 2 To 36
   strFromBase = ItemExtract (intBase - 1, strListTemp, @TAB)
   strMsgFromBase = strMsgFromBase : @CRLF : strFromBase : @TAB : "Base" : @TAB : intBase : @TAB : udfConvertFromBase (strFromBase, intBase)
Next
strMsgTitle = "Demo udfConvertFromBase (strFromBase, intFromBase)"
Message (strMsgTitle, strMsgFromBase)

Exit