pad.mattdiesel.co.uk

Snippet - _ToBase and _FromBase

_ToBase and _FromBase (AutoIt)

Conversations between bases.
Created 2014-04-08 19:18:04.532680 by Matt.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
Func _ToBase($iNumber, $iBase, $iPad = 1, $sCharSet = Default)
    Local Static $sDefCharSet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/"
    Local $sRet = "", $iDigit
 
    If $sCharSet = Default Then $sCharSet = $sDefCharSet
 
    Do
        $iDigit = Mod($iNumber, $iBase)
        $sRet = StringMid($sCharSet, $iDigit + 1, 1) & $sRet
        $iNumber = Int($iNumber / $iBase)
    Until ($iNumber = 0) And (StringLen($sRet) >= $iPad)
 
    Return $sRet
EndFunc   ;==>_ToBase
 
Func _FromBase($sNumber, $iBase, $sCharSet = Default)
    Local Static $sDefCharSet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/"
    Local $iRet = 0, $sChar
 
    If $sCharSet = Default Then $sCharSet = $sDefCharSet
 
    Do
        $iRet *= $iBase
        $sChar = StringLeft($sNumber, 1)
        $iRet += StringInStr($sCharSet, $sChar, 1) - 1
        $sNumber = StringTrimLeft($sNumber, 1)
    Until $sNumber = ""
 
    Return $iRet
EndFunc   ;==>_FromBase