# My own Binary() Function

## 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)
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
##### Share on other sites

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

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

## Create an account

Register a new account

• ### Similar Content

• Hi AutoIt Programmers, i wanna figure out how to use Binary functions in C# like:

BinaryMid
BinaryLen
IsBinary and other basic ones were too ez, but those two were hard to noob like me.

• By Stew
(Edited from original.  Please note that I AM NOT AN AUTOIT EXPERT.  I write code using Autoit frequently but I am no expert, especially when it comes to I/O.  So any remarks that start with "Why did you..." can be answered by referring to the first sentence.  This project was done in Autoit because of an interface I built to display the data.)
Attached is a program and ascii input file I wrote to read stock price data, convert it to binary and then read it back into the program in binary.  The goal was to show increased performance for reading the files in binary and provide a demo on how to read/write binary for int32, int64, double and strings for anyone who might find it helpful.  The results on my PC show the following:
Time to read ascii file only: 456.981951167202
Ascii read & process time: 6061.83075631701
Binary write file time: 14787.9184635239
Time just to read binary file: 42.418867292311
Binary read and process time: 4515.16129830537
A couple things to note:
1) The 32 MB ascii file took 10x longer to read than the 15 MB binary file.  Not entirely sure why.  Both were read into a buffer.
2) The Binary write takes a long time but I made no effort to optimize this because the plan was to write this file one time only so I don't mind if it takes longer to write this file.  I care much more about how long it takes to read the file because I will be reading it many times.
3) There was a modest gain in converting the ascii file to binary in terms of file size and reading speed.
So big picture... not sure it's worth the effort to convert the files to binary even though most of the data is numerical data in the binary file.  That was actually surprising as I expected there would be more of a difference.  Any ideas on how to get the binary data to read at a faster rate would be great.

binary.au3
2019_02_08.zip
• By FMS
Hello,
I'm trying to read a binary file to an array but couln't get it to work.
Also I coul not find any help in the forum around this subject whish was helpfull.
Is there any way it could be done?
I tried a lot of ways but maybe somebody know's the right way?
#AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7 #include <File.au3> #include <Array.au3> #include <AutoItConstants.au3> Local \$in=FileOpen("TEST_labels.idx1-ubyte",16) ; 16+0=Read binary Local \$data = FileRead(\$in) Local \$FileArray = BinaryToString(\$data,4) ;~ \$FileArray = StringSplit(\$BinarydData, @CRLF, 1+2) ;~ Local \$FileArray = StringRegExp(\$BinarydData, "[^\r\n]+", 3) FileClose(\$in) _ArrayDisplay(\$FileArray,"\$FileArray","",32) MsgBox(\$MB_SYSTEMMODAL, "", "\$FileArray = " & \$FileArray )
TEST_labels.idx1-ubyte

• I'm searching a way to do xor and shift and if possible also other operations. Thanks in advance for the replies.
• By rudi
Hello.
I'm too stupid to see my mistake:
To investigate the internal "dictionary" of TIFF files I'd like to read in the files in binary mode and to check, if there are more than one pages "in" this TIFF.
Notepad++, "View as Hex" is presenting the first bytes as "49 49 2a 20 08 20 20 20 12" for the TIF attached to this posting
The "TIFF Header Format" is easy:
Offset 00h, 2 Byte = Byte Order, "II"=intel, "MM"=motorola. (I = 0x49)
--> II
Offset 02h, 2 Byte = Version Nr.
Offset 04h, 4 Byte = pointer to first IFD entry

Howto read and analyse the binary content correctly? This is my messy, not operational code:

\$sampleTiff="H:\daten\tif\11\11\111111.TIF" \$h=FileOpen(\$sampleTiff,16) \$content=FileRead(\$h) ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : \$content = ' & \$content & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console FileClose(\$h) \$type=VarGetType(\$content) ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : \$type = ' & \$type & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console \$ToString=BinaryToString(\$content) ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : \$ToString = ' & \$ToString & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console ConsoleWrite(@CRLF & @CRLF) \$content=StringTrimLeft(\$content,2) ; cut off the leading "0x" ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : \$content = ' & \$content & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console for \$i = 1 to 8 step 8 \$next=StringMid(\$content,\$i,2) ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : \$next = ' & \$next & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console \$Chr=BinaryToString(\$next) ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : \$Chr = ' & \$Chr & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console ConsoleWrite(@CRLF & "---" & @CRLF) Next Regards, Rudi.
111111.TIF
×

• Wiki

• Back

• #### Beta

• Git
• FAQ
×
• Create New...