Jump to content
Sign in to follow this  
WideBoyDixon

Converting decimal numbers to another base

Recommended Posts

WideBoyDixon

Could be used to convert to binary (for example). Also allows padding of the result.

ConsoleWrite(_ToBase(15, 2, 8) & @CRLF)
ConsoleWrite(_ToBase(257, 3) & @CRLF)
ConsoleWrite(_ToBase(255, 4) & @CRLF)
ConsoleWrite(_ToBase(932, 8) & @CRLF)
ConsoleWrite(_ToBase(65535, 16, 8) & @CRLF)

Exit

Func _ToBase($iNumber, $iBase, $iPad = 1)
    Local $sRet = "", $iDigit
    Do
        $iDigit = Mod($iNumber, $iBase)
        If $iDigit < 10 Then
            $sRet = String($iDigit) & $sRet
        Else
            $sRet = Chr(55 + $iDigit) & $sRet
        EndIf
        $iNumber = Int($iNumber / $iBase)
    Until ($iNumber = 0) And (StringLen($sRet) >= $iPad)
    Return $sRet
EndFunc

WBD

  • Like 1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×