Sign in to follow this  
Followers 0
LarryDalooza

Binary to Integer

9 posts in this topic

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.


f_mrcleansmalm_77ce002.jpgAutoIt has helped make me wealthy

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Something like this?

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

Visit the SciTE4AutoIt3 Download page for the latest versions  - Beta files                                How to post scriptsource        Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
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.


f_mrcleansmalm_77ce002.jpgAutoIt has helped make me wealthy

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

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 this post


Link to post
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)))

Instead of...

$a = BinaryLen($SomeData)

SendData(Binary($a))

I am hoping for a simple intuitive...

$a = Binary(12345)

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

Lar.


f_mrcleansmalm_77ce002.jpgAutoIt has helped make me wealthy

Share this post


Link to post
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 this post


Link to post
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.


f_mrcleansmalm_77ce002.jpgAutoIt has helped make me wealthy

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0