# Binary digits stuff...

## Recommended Posts

Basically, StringToBinary was confusing me, as it was returning stuff like 0x3234 for 42, when i expected 010101...

so i wrote this!

very simple: BinaryEx.au3

and a little GUI for converting: BinaryConverter.au3

Thought it might come in handy to someone somewhere... so released it.

Thanks to sublime, who had the job of explaining StringToBinary....

MDiesel

##### Share on other sites

i did thought about something like this before, for using it with my Supernet calculator. but when it comes to "how much bits are needed to represent this ammount of ips i remembered about good old maths:

```Func convert(\$a)
if \$a < 2 then Return 0
Return Ceiling(Log(\$a)/Log(2))
EndFunc```

it can be used to get the ammount of bits used in an subnetmask to represent a ip range for example:

if you need 16 ipaddresses the function returns you 4 because you need 4 bits to create 16 combinations :-)

maybe this comes in handy for you.

its amazing how far people can be away from each other having slightly similar ideas

`\$a=StringSplit("547275737420796F757220546563686E6F6C75737421","")For \$b=1 To UBound(\$a)+(-1*-1*-1)step(2^4/8);&\$b+=1*2/40*µ&Asc(4)Assign("c",Eval("c")&Chr(Dec(\$a[\$b]&\$a[\$b+1])))''Chr("a")&"HI"Next;time_U&r34d,ths,U-may=get\$the&c.l.u.e;b3st-regards,JRSmile;MsgBox(0x000000,"",Eval("c"));PiEs:d0nt+*b3.s4d.4ft3r.1st-try:-)`

##### Share on other sites

Indeed tis confuseing, i had to resort to using this function for extracting numbers from binary:

```Func _BinaryToInt(\$4bytes)
\$Integer = DllStructCreate("int")
\$Binary = DllStructCreate("byte[4]", DllStructGetPtr(\$Integer))
DllStructSetData(\$Binary, 1, \$4bytes)
Return DllStructGetData(\$Integer, 1)
EndFunc```

Click here for the best AutoIt help possible.Currently Working on: Autoit RAT

##### Share on other sites

@JRSmile

kk,

thats useful, I would have done it something more like:

```Func GetBinLength (\$Num)
Local \$i = 1
Local \$Return = 0
Do
\$Return += 1
\$i *= 2
Until \$i > \$Num
Return \$Return
EndFunc```

so thanks! your way is a lot better, I haven't looked much at maths functions...

oh and, why can't I seem to make a for loop with step *2? is this just not allowed?

```Func GetBinLength (\$Num)
Local \$Return = 0
For \$i = 1 to \$Num Step *2
\$Return += 1
Next
Return \$Return
EndFunc```
Edited by mdiesel

##### Share on other sites

Sorry Marlo, started replying before you posted...

once again my way is less efficient (i think) but I can understand it!! I don't know much about dll's either, so my code just does string split "" and if 1 then add to the number!!

EDIT: I think I misunderstood you sorry....

what exactly does it do? I've tested but can't work it out, what is the input supposed to be?

Edited by mdiesel

##### Share on other sites

Take a look here you'll see the logic behind converting decimal to binary and vice versa.

##### Share on other sites

Hey mdiesel, nice little app there. The reason we don't use 010011 numbers or strings in computing is because it is much easier to describe all the values in one or several bytes. The octal (8-base, one byte) an hexadecimal (16-base, two bytes) is much more convenient for this.

Say if you wanted to write the value 0000 0100 0101 0111 you would just write 0x457 .. (1+2+4+16+64+1024)

This is easier for the computer to understand because it doesn't need to translate a string into a value to put into memory.

##### Share on other sites

Uh, octal and hexadecimal has nothing to do with 1 byte or 2 bytes. Hex can show just one byte (actually, just like this: 0x10).

The main thing I think people are having problems understanding is that the Binary that is in Autoit isn't the binary numbering system, but the raw bytes that would normally be kept in memory, and is usually hidden from users. A string is nothing but a formatted output for the Binary representing it. The same applies for all values we take for granted.

Binary as it's name makes sense to me. What else would you call it, StringToRawMemoryValues? Just remember, Binary doesn't mean the binary number system, but the raw data as the computer see's it.

Edit: Grammar ftw!

Edited by SkinnyWhiteGuy

##### Share on other sites

It makes sense once its explained, and this is released to work with the number system.

@AuthenticCity

Took a while, but heres my autoit version.

```\$input = 42
\$Count = ""
While (\$input > 0)
\$Count &= Mod (\$Input, 2)
If Not IsInt (\$input/2) Then \$Input -= 1
\$input /= 2
Wend
MsgBox (48, "Test", \$Count); should be 010101```

##### Share on other sites

mdiesel,

Negative numbers will pose a problem, and the Bitxx() functions only work on 32-bits currently. (see my signature for solutions to both)

Also, if you want to check if a *string* is all 0's or 1's, this will work:

`StringRegExp("01010","^[01]+\$",0)`

I haven't written a _BinaryStrToNum() function yet as I haven't come across anything where I need to do such a conversion, but it's quite possible to do up to 64-bit calculations, though anything in the 64th bit position will automatically make the number negative.

##### Share on other sites

```\$input = 42
\$Count = ""
While (\$input > 0)
\$Count &= Mod (\$Input, 2)
If Not IsInt (\$input/2) Then \$Input -= 1
\$input /= 2
Wend
MsgBox (48, "Test", \$Count); should be 010101```
\$Count contain the reverse string. You'll need to use _StringReverse to get the correct binary presentation.

##### Share on other sites

Sorry Marlo, started replying before you posted...

once again my way is less efficient (i think) but I can understand it!! I don't know much about dll's either, so my code just does string split "" and if 1 then add to the number!!

EDIT: I think I misunderstood you sorry....

what exactly does it do? I've tested but can't work it out, what is the input supposed to be?

Well i use the function in my TCP servers because when i send or recieve information on my server i prefix a string with an integer (to use as a command)

So to read the command i'll use something like:

`\$iCmd = _BinaryToInt(BinaryMid(\$BinaryVar, 1, 4))`

This just reads the first 4 bytes from a piece of binary and converts it into an integer

Click here for the best AutoIt help possible.Currently Working on: Autoit RAT

##### Share on other sites

right, just got back and...

@Ascendant

yep, makes my last function practically pointless (as is now said in the script)... I don't expect it to work with negatives or 64 bits... It was only supposed to be simple, and as I have said, it has been done before (and probably will be again). Thanks for the info though.

@Authenticity

ah... I see exactly what you mean, It's couse I started at 1 and added on the way up... How come I'm getting the right results then?

I could do string reverse, or just: (Doesn't give the right results though...)

```\$input = 42
\$Count = ""
While (\$input > 0)
\$Count = Mod (\$Input, 2) & \$Count
If Not IsInt (\$input/2) Then \$Input -= 1
\$input /= 2
Wend
MsgBox (48, "Test", \$Count); should be 010101```

@Marlo

oh... I get it now.

##### Share on other sites

Well, now it's OK. \$Count &= something is not the same as \$Count = Something & \$Count, and it should be 101010 because it's from the most significant to the least significant.

##### Share on other sites

Well, now it's OK. \$Count &= something is not the same as \$Count = Something & \$Count, and it should be 101010 because it's from the most significant to the least significant.

Yep got that... Thanks authenticity, you learn something new every day (even in the school holidays )

Thanks to everyone else too!

##### Share on other sites

Well, since I just had to do this the other day, here's a converter for Autoit's Binary to numerical binary.

```Func BinaryToBits(\$bin)
Local \$output
For \$i = 1 To BinaryLen(\$bin)
For \$j = 7 To 0 Step -1
\$output &= BitAND(BitShift(BinaryMid(\$bin, \$i, 1), \$j), 1)
Next
Next
Return \$output
EndFunc```

##### Share on other sites

nicely done...

I probably would have gone with binary to string and then string to binary....

completely rewritten both scripts (OK, not completely then...) anyway, re done most functions with new info.

## Create an account

Register a new account

×

• Wiki

• Back

• Git