# Convert Dec to Binary - but Ones and Zeros

## Recommended Posts

Hi All,

Trying to convert a number to binary zeros and ones but I'm getting a result I don't understand and looks more like hex than binary.

Here's my basic code:

```\$myNum = 11
\$myNumBin = Binary(\$myNum)
MsgBox(0, "Binary result", \$myNumBin)```

What I want is "1011", what I get is 0x0B000000.

Thanks!

Edited by WoodGrain
Wrong syntax

##### Share on other sites

Try this:

```MsgBox(0, "", DecToBase(11, 2))

Func DecToBase(\$iInt, \$iBase) ; for bases 2 to 9
Local \$iRem, \$sRet = ''
While \$iInt > \$iBase -1
\$iRem = Mod(\$iInt, \$iBase)
\$sRet = \$iRem & \$sRet
\$iInt = Int((\$iInt - \$iRem) /\$iBase)
WEnd
Return \$iInt & \$sRet
EndFunc ;==> DecToBase```

To reverse the process you can use this:

```MsgBox(0, "", BaseToDec('1011', 2))

Func BaseToDec(\$sInt, \$iBase) ; bases 2 to 9 only
Local \$iRet = 0, \$iLen = StringLen(\$sInt)
For \$i = 1 To \$iLen
\$iRet += StringMid(\$sInt, \$i, 1) * \$iBase ^ (\$iLen - \$i)
Next
Return \$iRet
EndFunc ;==> BaseToDec```

Edited by czardas

##### Share on other sites

That worked perfectly, thanks!

I note it does bases 2 - 9, would it be complex to allow up to base 16 so I can convert it to hex as well?

##### Share on other sites

It's not so complicated, but unless you need bases 11 to 15, you might as well use Hex() and Dec(). I don't have much time this morning. You could try this old code of mine:
http://www.czardas.co.uk/repository/math10.au3

The code I posted in this thread can also be modified to convert larger numbers (up to Int64) by replacing the operators with operator64 functions (see my signature). Currently, these conversion functions only work for doubles ~ up to 15 decimal digits.

Edited by czardas

##### Share on other sites

No worries, thanks very much.

Actually, yeah, hex() works as expected

```\$myNum = 30
\$myNumHex = Hex(\$myNum, 2)
MsgBox(0, "Hex result", \$myNumHex)```

Returns 1E

Edited by WoodGrain
Update after testing

##### Share on other sites
18 hours ago, czardas said:

\$iInt = Int((\$iInt - \$iRem) /\$iBase)

This line could be written like this with no change in output.

`\$iInt = Int(\$iInt / \$iBase)`

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

##### Share on other sites

@BrewManNH Well spotted! By all means change it.

Instead of that, if I implement Operator64 functions, Int() could be removed. It should not really be needed here neither, but because of rounding inaccuracies, I added it to be on the safe side. That's also why I decided on division by a real factor (subtracting the remainder first): I don't trust double precision to always return an accurate result.

It's a while since I wrote this and I'm trying to remember everything I was thinking at the time. I must have written it that way because of the aforementioned reasons.

Edited by czardas

##### Share on other sites

I have modified this method to be compatible with all numbers within the positive Int64 range. This example requires operator64.au3 (see link in my signature).

CODE MOVED HERE:

Edited by czardas

## Create an account

Register a new account

• ### Similar Content

• 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 DigiBox
Hello everyone,
I decided to rewrite/convert some autohotkey scripts to autoit. I know Ahk pretty well, but I'm obviously new at autoit.
I've done my best with this one and the problem I'm getting now is the error message "Error: Unknown function name".
Forgive me if it's something simple, but like I said, I'm completely new at this (been at it for one day, while ahk for years).
Here is the autoit script:

#RequireAdmin If WinExists(@ScriptName) Then Exit AutoItWinSetTitle(@ScriptName) \$slowness = IniRead ("extra.ini", "slownesss", slowness, "0") \$slowdelay = IniRead ("extra.ini", "slownesss", slowdelay, "0") Global \$Paused HotKeySet("{PAUSE}", "TogglePause") HotKeySet("{DELETE}", "Terminate") \$track = 0 while 1 global \$pos2 = mousegetpos() Local \$coord1 = PixelSearch (511, 388, 511, 424, 0xBD302E, 30) If Not @error Then exitloop EndIf wend For \$redloop = 1 to 10 step 1 global \$pos2 = mousegetpos() local \$coord2 = PixelSearch (511, 388, 511, 424, 0xBD302E, 30) If Not @error Then \$track = 1 EndIf If @error Then ExitLoop EndIf If \$redloop > 0 Then \$fade = \$redloop / 100 EndIf \$move = \$slowness + fade if \$track = 1 Then global \$pos1 = mousegetpos() \$newx1 = \$pos1-\$xpos2 \$newx2 = \$pos2-\$xpos1 \$movex = \$newx2/move _MouseMovePlus(\$movex,0) global \$pos2 = mousegetpos() \$track = 0 EndIf Next while 1 sleep 1 local \$coord3 = PixelSearch (511, 388, 511, 424, 0xBD302E, 30) If @error Then exitloop EndIf WEnd Func TogglePause() \$Paused = NOT \$Paused While \$Paused sleep(100) WEnd EndFunc ; mousemove------------------------------------------ Func _MouseMovePlus(\$X, \$Y,\$absolute = 0) global \$MOUSEEVENTF_MOVE = 1 Local \$MOUSEEVENTF_ABSOLUTE = 32768 DllCall("user32.dll", "none", "mouse_event", _ "long", \$MOUSEEVENTF_MOVE + (\$absolute*\$MOUSEEVENTF_ABSOLUTE), _ "long", \$X, _ "long", \$Y, _ "long", 0, _ "long", 0) EndFunc Func Terminate() Exit 0 EndFunc
And here is the original Ahk script which I wrote (and works perfectly) :

#SingleInstance force CoordMode, pixel,screen CoordMode, mouse,screen SetBatchLines -1 IniRead, slowness, extra.ini, slownesss, slowness IniRead, slowdelay, extra.ini, slownesss, slowdelay mouseXY(x,y) { DllCall("mouse_event",uint,1,int,x,int,y,uint,0,int,0) } track = 0 loop { mousegetpos, xpos2, ypos2 PixelSearch, Px, Py, 511, 388, 511, 424, 0xBD302E, 30, fast RGB if errorlevel = 0 { sleep slowdelay gosub, red } } red: loop, 10 { mousegetpos, xpos2, ypos2 PixelSearch, Px, Py, 511, 388, 511, 424, 0xBD302E, 30, fast RGB if errorlevel = 0 track = 1 if errorlevel = 1 break if (a_index > 0) fade := (a_index / 100) move := (slowness + fade) if track = 1 { MouseGetPos, xpos1, ypos1 newx1 := (xpos1-xpos2) newx2 := (xpos2-xpos1) mousexy(newx2/move, 0) mousegetpos, xpos2, ypos2 track = 0 } } loop { sleep 1 PixelSearch, Px, Py, 511, 388, 511, 424, 0xBD302E, 30, fast RGB if errorlevel = 1 return } return
Any help about the error is greatly appreciated and perhaps something else you may spot.

• I need help turning this string "20180913221626" into a formatted time string.
I need to go from this: 20180913221626
to this: 09/13/2018 10:16 PM
I do not always know what the date will be so I can not just use a variable I need to actually convert/format.
I did see an older post in the forms that was basically the same question only the other guy did not post the working code and I can not figure out how to use _AD_GetObjectProperties properly to get what I want.
As always any help would be appreciated.
Here is the code I use to find the date, but it always gives me the unformatted version.
;retrieve the items object \$oItem = \$oOutlook.Session.GetItemFromID(\$aItems[1][0], Default) \$oItem.GetInspector \$eSentOn = \$oItem.SentOn ; When was the email sent? MsgBox("", "Sent On of the email", \$eSentOn) ;******************************************************************************* ; Formats the date and time from the email ;******************************************************************************* ;Local \$fDatenTime = _DateTimeFormat(\$eSentOn, 1) ;MsgBox("", "Formatted email time", \$fDatenTime) \$aProperties = _AD_GetObjectProperties(\$eSentOn) _ArrayDisplay(\$aProperties, "Did the conversion work?")
• By jtpr
Has anybody ever imported a VuGen recording into AutoIT?  I like the recording cabability of VuGen and thought it would be great to use and then pull it into AutoIT to compile into an EXE.

Jim
• 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
×

• Wiki

• Back

• #### Beta

• Git
• FAQ
• Our Picks
×
• Create New...