Jump to content

My own Binary() Function


Pickpocketz88
 Share

Recommended Posts

Func _Binary($Int)
    ;Uncomment To Only Accept Integers
    #cs
    If IsInt($Int) = 0 Then
        Return 0
    EndIf
    #ce

    If $Int < 0 Then ;Negative Numbers Will Break The Function
        Return 0000
    EndIf

    Local $Integer = $Int
    Dim $Bin[1] = [Mod($Integer, 2)]
    Local $Counter = 1

    Do
        $Integer = Floor($Integer / 2)
        _ArrayAdd($Bin, Mod($Integer, 2))
    Until $Integer = 0

    _ArrayReverse($Bin) ;Reverses The Array Because As Is, The Product Is Backwards

    ;A Loop To Remove Any Preceding 0's or Add 0's To Keep At Least Four Digits
    Select
        Case $Int <= 1
            $Integer = "00" & _ArrayToString($Bin, "")
        Case $Int = 2 Or $Int = 3
            $Integer = "0" & _ArrayToString($Bin, "")
        Case $Int >= 8
            $Integer = StringTrimLeft(_ArrayToString($Bin, ""), 1)
        Case Else
            $Integer = _ArrayToString($Bin, "")
    EndSelect
    ;You Can Comment It Out Without Anything Else Having A Problem

    Return $Integer
EndFunc

I made this because I was writing something that I could use to play with Bitwise Operations and using Binary() by itself wasn't helping.

It's very basic and will only take positive integers because that's all I needed but I'm sure with a little tweaking you could make it fit with negative and float types.

It returns a string essentially but doesn't pose a problem when just changing numbers into binary digits.

Example: If you were to do _Binary(5) you would get "0101" and _Binary(8) would return "1000"

Between this last sentence and here I've changed this about a half dozen times to refine it a bit because without it checking if your number is < 0 it would break if a negative number was inserted and it wouldn't even have a problem if you put in Float Values, Regular or Special Characters but that negative value will do the trick lol.

Anyway, I hope someone finds some use of this and thank you for reading!

-Pick

Edited by Pickpocketz88
Added Comment For User Info
Link to comment
Share on other sites

Make your life simple!

Local $n = 53
Local $sBin = _UintToString($n, 2)
ConsoleWrite($n & " evaluates as binary " & $sBin & @LF)

Func _UintToString($i, $base)
    Return DllCall("msvcrt.dll", "wstr:cdecl", "_ui64tow", "uint64", $i, "wstr", "", "int", $base)[0]
EndFunc   ;==>_UintToString

You can plug any value to $n and see what it yields.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

Return DllCall("msvcrt.dll", "wstr:cdecl", "_ui64tow", "uint64", $i, "wstr", "", "int", $base)[0]

lol, nice, I had no idea you could do it that way, with the array thingie at the end, I usually....

$v = DllCall($dll)

Return $v[4]

I'm off to shortening a bunch of scripts, thanks :)

Some guy's script + some other guy's script = my script!

Link to comment
Share on other sites

It's OK as long as you're sure enough that tyhe DllCall will always succeed, else grab the dustpan fast.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

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

×
×
  • Create New...