Sign in to follow this  
Followers 0
MuffettsMan

Decimal to Binary Converter...

5 posts in this topic

it was simple enough when i converted it manually (top of my example code) but when i tried to make a function that would take any size number and convert it.... i failed. closest i could get is getting it to convert all but the last digit but even still there must be a more logical approach to this ^_^

#include <Math.au3>
#AutoIt3Wrapper_run_debug_mode=Y

$I_Var = InputBox('Decimal to Binary', 'Enter a number between 0-15')

$num = $I_Var


$8_Result = int($I_Var/8)
    if ($8_Result) then $I_Var = $I_Var -8
$4_Result = int($I_Var/4)
    if ($4_Result) then $I_Var = $I_Var -4
$2_Result = int($I_Var/2)
    if ($2_Result) then $I_Var = $I_Var -2
$1_Result = int($I_Var/1)

Func Decimal2Binary($num)
    $x = int(Sqrt($num))
    ; MsgBox(0,'sqrt',$x)
    
    ; get the sqr to work down from...
    $i = 0
    $sqr = 1
    if $x > 1 Then
        while $i < $x
            $sqr = $sqr * 2
            $i = $i + 1
        WEnd
    EndIf
    
    ; now loop all the way to 1
    $i = 0
    dim $binary
    while $i <= $x
        if $sqr > 2 then
            $bin = int($num/$sqr)
        EndIf
        $num = $num - $sqr
         
        $sqr = $sqr/2   
        
            
        $binary = $binary & $bin
        $i = $i + 1
    WEnd
    
    ; $bin = int($num/1)
    ; $binary = $binary & $bin
        
    MsgBox(0,'',"binary: " & $binary)
    
EndFunc
    
    
MsgBox(0,'','Binary: ' & $8_Result & $4_Result & $2_Result & $1_Result)

Decimal2Binary($num)

Don't let that status fool you, I am no advanced memeber!

Share this post


Link to post
Share on other sites



WoW... lol yah thats a bit over my head (at least 'how' its working) but ty ty WBD it works well now to see if i can disect it and figure out what the heck kind of magic is going on ^_^

thx again


Don't let that status fool you, I am no advanced memeber!

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Seems like you're making stuff more complicated than they need to be.

This method see which numbers in the 2^n series that can be subtracted from the decimal and if it can it adds a 1 and if 0 in the other case.

The function looks like this.

$Number=InputBox("Dec2Bin","Decimal to convert to Binary",0)
MsgBox(0, $Number, Dec2Bin($Number))

Func Dec2Bin($Num)
    Local $Bin
    Local $iStart = 2 ^ Int(Log($Num) / Log(2))
    For $i = 0 To Int(Log($Num) / Log(2))
        If $Num - $iStart >= 0 Then
            $Bin &= "1"
            $Num -= $iStart
        Else
            $Bin &= "0"
        EndIf
        $iStart/=2
    Next
    Return $Bin
EndFunc;==>Dec2Bin

^_^

Edit: Slow, nice script there WideBoyDixon ;)

Edited by monoceres

Broken link? PM me and I'll send you the file!

Share this post


Link to post
Share on other sites

WoW... lol yah thats a bit over my head (at least 'how' its working) but ty ty WBD it works well now to see if i can disect it and figure out what the heck kind of magic is going on ^_^

thx again

It constructs the number from right to left. To do so it continually takes the remainder from dividing by two (so zero or 1) and prepends that to the result. It then divides the original number by 2 and starts again. So, for example, for 11:

Mod(11, 2) = 1 : Int(11/2) = 5

Mod(5, 2) = 1 : Int(5/2) = 2

Mod(2, 2) = 0 : Int(2/2) = 1

Mod (1, 2) = 1 : Int(1/2) = 0 -> Stop

Reading from the bottom up gives 1011 which is the representation of binary. Since you only want binary you *could* use bit-wise operations if you want something a bit faster:

Global $iNumber = 11
Do
    $iNumber = Number(InputBox("Dec2Bin", "Enter a decimal number", $iNumber))
    MsgBox(64, "Result for " & $iNumber, Dec2Bin($iNumber))
Until $iNumber = 0
Exit

Func Dec2Bin($iNumber)
    Local $sRet = ""
    Do
        $sRet = BitAND($iNumber, 1) & $sRet
        $iNumber = BitShift($iNumber, 1)
    Until $iNumber = 0
    Return $sRet
EndFunc

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  
Followers 0