Jump to content

Binary representation of a number


Recommended Posts

I did some searching, and some looking at the help files.

I would have expected Binary(12) to output 1100, but I found out there's binary, and then there's binary :unsure:

So I guess after having been enlightened, the question becomes Has anyone made a Binary() function that outputs Numbers in Binary format?

Or is there a function/option/flag I've overlooked somewhere?

Thanks :>

Link to comment
Share on other sites

Try this:

#include <String.au3>
MsgBox(0, "Test", Integer2Binary(12))

Func Integer2Binary($in)
    If Not IsInt($in) Then Return SetError(1, 0, "Error")
    If $in = 0 Then Return 0
    Local $bin
    While $in > 0
        $bin &= Mod($in, 2)
        $in = Floor($in / 2)
;~      $in = BitShift($in, 1)
    WEnd
    Return(_StringReverse($bin))
EndFunc

Br,

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

Another version, linear on size of input (works on large binary data as well, not only on integers:

$n = Binary(Random(0, 123456789, 1))
ConsoleWrite($n & @LF)
ConsoleWrite(_BinaryDump($n) & @LF)

Func _BinaryDump($n)
    $n = Binary($n)
    $n = StringReplace($n, '0', '0000')
    $n = StringReplace($n, '1', '0001')
    $n = StringReplace($n, '2', '0010')
    $n = StringReplace($n, '3', '0011')
    $n = StringReplace($n, '4', '0100')
    $n = StringReplace($n, '5', '0101')
    $n = StringReplace($n, '6', '0110')
    $n = StringReplace($n, '7', '0111')
    $n = StringReplace($n, '8', '1000')
    $n = StringReplace($n, '9', '1001')
    $n = StringReplace($n, 'A', '1010')
    $n = StringReplace($n, 'B', '1011')
    $n = StringReplace($n, 'C', '1100')
    $n = StringReplace($n, 'D', '1101')
    $n = StringReplace($n, 'E', '1110')
    $n = StringReplace($n, 'F', '1111')
    $n = StringReplace($n, '0000x', '0b')
    return($n)
EndFunc

Note: it displays 0b.... to denote binary instead of 0x.... to denote hex, but change it according to your needs.

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...