Jump to content

Converting decimal numbers to another base

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)


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


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
  • Recently Browsing   0 members

    No registered users viewing this page.

  • Create New...