Jump to content

Number to binary


Recommended Posts

I'm looking for a function that will convert a number (ie 33) into a binary number (ie 100001). The Bin and Bit functions in AutoIt don't seem to do it, unless I'm not trying the right ones, so I wrote this function:

MsgBox(0,"33 as binary",_ToBin(33))
;Function name:            _ToBin
;Function author:          james3mg
;Function description:     _ToBin converts any number to a binary string (remember to precede a hex number with 0x).
;                          Digits after a decimal point are ignored.  I can't make it error, so there is no error checking.
;                          It handles the string passed to it like Number() - that is, 0 if string begins with non-number, stripped non-numbers if string begins with a number.
Func _ToBin($_aNum=0)
    Local $i=1, $_returnBin=""
    Do
        If BitAND(Abs($_aNum),$i) = $i Then
            $_returnBin=1&$_returnBin
        Else
            $_returnBin=0&$_returnBin
        EndIf
        $i=$i*2
    Until $i>Abs($_aNum)
    If $_aNum < 0 Then $_returnBin *=-1
    Return Number($_returnBin)
EndFunc

Is there a better/more concise way of doing this that someone's developed, or (even better), could someone point me to the built-in function I'm probably missing that does this?

Thanks all :)

"There are 10 types of people in this world - those who can read binary, and those who can't.""We've heard that a million monkeys at a million keyboards could produce the complete works of Shakespeare; now, thanks to the Internet, we know that is not true." ~Robert Wilensky0101101 1001010 1100001 1101101 1100101 1110011 0110011 1001101 10001110000101 0000111 0001000 0001110 0001101 0010010 1010110 0100001 1101110
Link to comment
Share on other sites

If the input is Integer, here's the "bit" way (return type is a String):

Func _ToBin($int)
    $bin=""
    For $i = 1 To 32
        $bin= BitAND($int, 1) & $bin
        $int= BitShift($int, 1)
    Next
    Return $bin
EndFunc
Edited by Siao

"be smart, drink your wine"

Link to comment
Share on other sites

If the input is Integer, here's the "bit" way (return type is a String):

Nice, that should work great - I assume 32 represents the max. processed bits available (AutoIt being a 32-bit app)?

Thanks!

"There are 10 types of people in this world - those who can read binary, and those who can't.""We've heard that a million monkeys at a million keyboards could produce the complete works of Shakespeare; now, thanks to the Internet, we know that is not true." ~Robert Wilensky0101101 1001010 1100001 1101101 1100101 1110011 0110011 1001101 10001110000101 0000111 0001000 0001110 0001101 0010010 1010110 0100001 1101110
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...