Jump to content

Decimal to Binary conversion


Recommended Posts

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?

Link to comment
Share on other sites

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

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?

Link to comment
Share on other sites

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..

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...