Jump to content
Sign in to follow this  
laidback01

Decimal to Binary conversion

Recommended Posts

laidback01

hey, I'm working with a script where I want to have options - and the options settable via command line. Instead of listing each option out and reading that, I thought it'd be nice to be able to do something like C:\thescript.exe 11.

The script would then convert 11 to binary or 001011 and each bit would stand for either doing or not doing something. To me, this will be pretty easy to script and all, save for I can't seem to sort out how to convert from decimal to binary - I'd have thought this was done - I see dec to hex and vice versa, but don't see a binary conversion module.

Binary doesn't do what I'd hoped - for example with the number 4, I get 0x040000 for my binary response. this is not what I want. 4 (to me) should equal 0100.

Hopefully someone else out there knows what I'm looking for...

Any suggestions?

Share this post


Link to post
Share on other sites
laidback01

Nevermind, I wrote this to do what I want - only need 6 bits anyway, so this little func will be perfect.

Func bin($decimal)
    local $retVal[6], $startVal=String($decimal), $work, $work2
    ; in this case we'll have only 6 part arrays.
    for $i = 5 to 0 step -1
        $retVal[$i] = Int(Mod($decimal,2))
        $decimal = $decimal / 2
    next
    return $retVal
EndFunc
Edited by laidback01

Share this post


Link to post
Share on other sites
weaponx

This is what you need:

;Perform commands 1-5
;00111111
$num = 63

;Perform commands 2 + 4
;00001010
$num = 10

If BitAND(1, $num) Then
    MsgBox(0,"","Performing command 1")
EndIf

If BitAND(2, $num) Then
    MsgBox(0,"","Performing command 2")
EndIf

If BitAND(4, $num) Then
    MsgBox(0,"","Performing command 3")
EndIf

If BitAND(8, $num) Then
    MsgBox(0,"","Performing command 4")
EndIf

If BitAND(16, $num) Then
    MsgBox(0,"","Performing command 5")
EndIf

Also the command Binary() not working?

Share this post


Link to post
Share on other sites
laidback01

well, Binary doesn't do what I expect. If I take Binary(4), my return is 0x040000, and if I do my little gig, bin(4), gives 000100 which is what I want.

this way I can do

if $choice[0] then

dosomething()

endif

...

if $choice[1] then

dosomethingelse()

endif

etc on down the line... or even check for it in the func itself..

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  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.