# Binary to Integer

## Recommended Posts

I have a number and I want to convert it to 4 bytes (for TCP) and then from 4bytes back to a number...

so basically...

\$a = Binary(6000)

How do I get \$a to be an "Number" data type equal to 6000?

\$a = Binary(6000)

;do something to \$a here to make it 6000 again...

MsgBox(0,"",\$a)

should show a msgbox with "6000" in it.

thanks guys... I may find it soon, but I just need some help in case I don't get it soon. Time crunch here...

Lar.

AutoIt has helped make me wealthy

##### Share on other sites

Something like this?

```\$a = Binary(String(6000))
\$a = BinaryToString(\$a)
MsgBox(0, "", \$a)```
Edited by Jos

Live for the present,
Dream of the future,
Learn from the past.

##### Share on other sites

Something like this?

```\$a = Binary(String(6000))
\$a = BinaryToString(\$a)
MsgBox(0, "", \$a)```
Almost, but I want it to fit into 4 bytes... so I send

Binary(6000) ; 4 bytes via TCP

And when I TCPRecv the 4 bytes I want to go back to a number 6000

so

String(123456)

would be 6 bytes

Binary(123456)

would be 4 bytes

I confuse myself... I wonder if Bit...() functions are in order.

Lar.

AutoIt has helped make me wealthy

##### Share on other sites

```; Integer
Local \$a = 123456
ConsoleWrite(\$a & @CRLF)

; To binary (4 bytes)
\$a = Binary("0x" & Hex(\$a))
ConsoleWrite(\$a & @CRLF)

; To integer
\$a = Dec(StringTrimLeft(\$a, 2))
ConsoleWrite(\$a & @CRLF)```

Edited by -Ultima-

[ WinINet.au3 | Array.au3 (Optimized) | _UnixTimeParse() ]

##### Share on other sites

Edit: NVM - You want little-endian byte-ordered fancy whizbang... as provided by SkinnyWhiteGuy.

Edited by PsaltyDS

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

##### Share on other sites

```; Integer
Local \$a = 123456
ConsoleWrite(\$a & @CRLF)

; To binary (4 bytes)
\$a = Binary("0x" & Hex(\$a))
ConsoleWrite(\$a & @CRLF)

; To integer
\$a = Dec(StringTrimLeft(\$a, 2))
ConsoleWrite(\$a & @CRLF)```
Would work, I suppose, but then I would impose on the client the need to do...

\$a = BinaryLen(\$SomeData)

SendData(Binary("0x" & Hex(\$a)))

\$a = BinaryLen(\$SomeData)

SendData(Binary(\$a))

I am hoping for a simple intuitive...

\$a = Binary(12345)

MsgBox(0,"",Bin2Int(\$a))

Lar.

AutoIt has helped make me wealthy

##### Share on other sites

This is probably the beast you were trying to avoid, but it does what you want it to...

```\$dllStruct1_Integer = DllStructCreate("int")
\$dllStruct1_Binary = DllStructCreate("byte[4]", DllStructGetPtr(\$dllStruct1_Integer))

DllStructSetData(\$dllStruct1_Integer,1,6000)
\$a = DllStructGetData(\$dllStruct1_Binary, 1)
ConsoleWrite(\$a & @CRLF)

\$dllStruct2_Integer = DllStructCreate("int")
\$dllStruct2_Binary = DllStructCreate("byte[4]", DllStructGetPtr(\$dllStruct2_Integer))

DllStructSetData(\$dllStruct2_Binary,1,Binary("0x71170000")) ; 1 more than 6000, Little-Endian Order
\$b = DllStructGetData(\$dllStruct2_Integer, 1)
ConsoleWrite(\$b & @CRLF)```

##### Share on other sites

This is probably the beast you were trying to avoid, but it does what you want it to...

```\$dllStruct1_Integer = DllStructCreate("int")
\$dllStruct1_Binary = DllStructCreate("byte[4]", DllStructGetPtr(\$dllStruct1_Integer))

DllStructSetData(\$dllStruct1_Integer,1,6000)
\$a = DllStructGetData(\$dllStruct1_Binary, 1)
ConsoleWrite(\$a & @CRLF)

\$dllStruct2_Integer = DllStructCreate("int")
\$dllStruct2_Binary = DllStructCreate("byte[4]", DllStructGetPtr(\$dllStruct2_Integer))

DllStructSetData(\$dllStruct2_Binary,1,Binary("0x71170000")) ; 1 more than 6000, Little-Endian Order
\$b = DllStructGetData(\$dllStruct2_Integer, 1)
ConsoleWrite(\$b & @CRLF)oÝ÷ Ûú®¢×)¶­"ëvØb±«­¢+ÙÕ¹ ¥¸É9Õ´ ÀÌØìÑåÑÌ¤(\$ÀÌØí±±MÑÉÕÐÉ}%¹ÑÈô±±MÑÉÕÑ
ÉÑ ÅÕ½Ðí¥¹ÐÅÕ½Ðì¤(\$ÀÌØí±±MÑÉÕÐÉ} ¥¹Éäô±±MÑÉÕÑ
ÉÑ ÅÕ½ÐíåÑlÑtÅÕ½Ðì°±±MÑÉÕÑÑAÑÈ ÀÌØí±±MÑÉÕÐÉ}%¹ÑÈ¤¤((%±±MÑÉÕÑMÑÑ ÀÌØí±±MÑÉÕÐÉ}   ¥¹Éä°Ä°ÀÌØìÑåÑÌ¤(%IÑÕÉ¸±±MÑÉÕÑÑÑ ÀÌØí±±MÑÉÕÐÉ}%¹ÑÈ°Ä¤)¹Õ¹```

Thanks!!!

Lar.

AutoIt has helped make me wealthy

##### Share on other sites

No problem, it's the way I figured to mimic Python's pack/unpack for a project I'm working on. Also makes a for a poor man's union, when you have to do something like that.

## Create an account

Register a new account