Jump to content

Converting decimal numbers to another base


WideBoyDixon
 Share

Recommended Posts

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

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...