Sign in to follow this  
Followers 0
Marlo

Concatenating binary?

2 posts in this topic

Hey guys. I've started work on a simple client/server app and i have hit a snag.

So i have my app set up to use TCP to send and receieve data between 2 apps and in order to correctly proccess the data i want to prefix any data sent with a flag so the client or server app will know what to do with the data. I tried doing something like the following (Added comments for info)

$Flag = 0x100 ;Will be 4 bytes long.
$Dat = "This is some data" ;Will be 17 bytes long

msgbox(0, "Flag Length", BinaryLen($Flag))
MsgBox(0, "Dat Length", BinaryLen($Dat))

$Pack = ($Flag) + Binary($Dat) ;I even tried converting the $Flag into binary but it just seemed to add more bits.

MsgBox(0, "Packet Length", BinaryLen($Pack))

MsgBox(0, "Trying to extract Flag", (BinaryMid($Pack, 1,4))) ;Try to extract the first 4 bytes from the packet. Fails.
MsgBox(0, "Trying to extract data", BinaryToString(BinaryMid($Pack, 5))) ;Try to extract the remaining bytes from the packet and convert to string. Works.

I could easily accomplish this using strings and just separating out the flags using stringinstr but i would prefer to get it working this way. So does anyone know why it won't extract out the flag?


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

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

This might help

$Flag = _IntBinary(Number(0x100)) ;Will be 4 bytes long.
$Dat = _StrBinary("This is some data") ;Will be 17 bytes long

Local $Len[2]=[StringLen($Flag),StringLen($Dat)]
msgbox(0, "Flag Length", $Len[0])
MsgBox(0, "Dat Length",$Len[1])

$Pack = $Flag & $Dat ;I even tried converting the $Flag into binary but it just seemed to add more bits.

MsgBox(0, "Packet Length", StringLen($Pack))
MsgBox(0, "Trying to extract Flag", _BinInteger(StringMid($Pack, 1,$Len[0]))) ;Try to extract the first 4 bytes from the packet. Fails.
MsgBox(0, "Trying to extract data", _BinStr(StringMid($Pack, $Len[0]+1,$Len[1]))) ;Try to extract the remaining bytes from the packet and convert to string. Works.



;===========Binary [0 & 1] Functions======================
Func _StrBinary($sString)
Local $Binary = ""
For $i = 1 To StringLen($sString)
$Binary &= _IntBinary(Asc(StringMid($sString, $i, 1)))
Next
Return $Binary
EndFunc

Func _BinStr($sBin)
Local $sWidth=Ceiling(StringLen($sBin)/ 8)
$sBin=StringFormat("%0"&$sWidth*8&"s", $sBin)
If IsInt(StringLen($sBin)/8)<>1 Then Return SetError(1,StringLen($sBin),-1)
Local $sOffset=StringLen($sBin)/8
Local $sBinString
Local $sString
For $i=1 To $sOffset
$sBinString=StringMid($sBin,(($i-1)*8)+1,8)
$sString&=Chr(_BinInteger($sBinString))
Next
Return $sString
EndFunc

Func _IntBinary($sInteger)
If $sInteger <= 0 Then Return SetError(1,$sInteger,-1)
Local $sParameter
While $sInteger> 0
$sParameter &= Mod($sInteger, 2)
$sInteger = Floor($sInteger/2)
WEnd
$sParameter = _StringOpposite($sParameter)
Return StringFormat("%08s", $sParameter)
EndFunc

Func _BinInteger($in)
Local $sInteger, $x, $i = 1, $aTmp = StringSplit(_StringOpposite($in), "")
For $x = 1 To $aTmp[0]
$sInteger += $aTmp[$x] * $i*(2^($x-1))
Next
$aTmp = 0
Return StringFormat('%d', $sInteger)
EndFunc

Func _StringOpposite($sString)
Local $sLen=StringLen($sString),$sReturn
For $i=$sLen To 1 Step -1
$sReturn&=StringMid($sString,$i,1)
Next
Return $sReturn
EndFunc

But what you are trying is of no use

or maybe im a begginer

The above is more complicated if you just want to use your native method then this works for me

$Flag = 0x100 ;Will be 4 bytes long.
$Dat = "This is some data" ;Will be 17 bytes long

msgbox(0, "Flag Length", BinaryLen($Flag))
MsgBox(0, "Dat Length", BinaryLen($Dat))

$Pack = ($Flag) & Binary($Dat) ;I even tried converting the $Flag into binary but it just seemed to add more bits.

MsgBox(0, "Packet Length", BinaryLen($Pack))

;Done 3 because of 0x100=256
MsgBox(0, "Trying to extract Flag", BinaryToString((BinaryMid($Pack, 1,3)))) ;Try to extract the first 4 bytes from the packet. Success
MsgBox(0, "Trying to extract data", BinaryToString(BinaryToString(BinaryMid($Pack, 4)))) ;Try to extract the remaining bytes from the packet and convert to string. Works.
Edited by PhoenixXL

My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

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