Created
June 19, 2013 10:44
-
-
Save MattDiesel/5813398 to your computer and use it in GitHub Desktop.
Convert to/from numeric bases/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment