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

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

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

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

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

Share on other sites

Thanks a ton, both of you

Create an account

Register a new account