Sign in to follow this  
Followers 0
-Ultima-

_BinaryCompare()

5 posts in this topic

#1 ·  Posted (edited)

; #FUNCTION# ===============================================================================
; Name ..........: _BinaryCompare
; Description ...: Compare binary data
; Syntax.........: _BinaryCompare($bData1, $bData2)
; Parameters ....: $bData1 - First binary data to compare
;                  $bData2 - Second binary data to compare
; Return values .: = 0 if $bData1 = $bData2
;                  < 0 if $bData1 < $bData2
;                  > 0 if $bData1 > $bData2
; Author ........: Ultima
; Modified.......:
; Remarks .......:
; Related .......: StringCompare
; Link ..........;
; Example .......; Yes
; ==========================================================================================
Func _BinaryCompare($bData1, $bData2)
    ; Find minimum length
    Local $iLength1 = BinaryLen($bData1), $iLength2 = BinaryLen($bData2), $iLength = $iLength1, $iDiff
    If $iLength2 < $iLength1 Then $iLength = $iLength2

    ; Compare
    For $i = 1 To $iLength
        $iDiff = Int(BinaryMid($bData1, $i, 1)) - Int(BinaryMid($bData2, $i, 1))
        If $iDiff Then Return $iDiff
    Next
    Return $iLength1 - $iLength2
EndFunc

Local $a = StringToBinary('announce'), $b = StringToBinary('info')
MsgBox(0, StringFormat('_BinaryCompare(%s, %s)', $a, $b), _BinaryCompare($a, $b))

So I was trying to compare two binary variants today, and found that I couldn't do so directly via the usual comparison operators, so I ended up rolling my own. It's a simple function, really. All it does is compare two pieces of binary data not unlike how StringCompare() would work. This is useful for cases where comparing data as strings isn't always possible (like when dealing with bencoded dictionaries whose keys can be arbitrary binary data -- not just plain ol' strings).

I don't expect too many people to find much use for this function, but I'll put it up anyway, just because :D

/me wonders if he should request a native way to compare binary variants to be built into AutoIt...

(Too lazy to create a new account for Trac... xD)

Edited by -Ultima-

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

Share this post


Link to post
Share on other sites



I don't expect too many people to find much use for this function, but I'll put it up anyway, just because :D

/me wonders if he should request a native way to compare binary variants to be built into AutoIt...

(Too lazy to create a new account for Trac... xD)

It's usefull for me. Thanks.

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

It's nice

Although why can't you do it like this

Local $a = StringToBinary('announce'), $b = StringToBinary('info')
ConsoleWrite( _BinCmp($a, $b) & @CRLF)

Func _BinCmp($b1, $b2)
    Local $h1 = Hex($b1), $h2 = Hex($b2)
    If $h1 = $h2 Then Return 0
    If $h1 > $h2 Then Return 1
    Return -1
EndFunc
Edited by Siao

"be smart, drink your wine"

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

lol I guess you could do it like that. I tried all sorts of things before coming up with my "solution," including Hex(), but they never worked. Honestly, when I tested Hex() yesterday, it appeared like it was only comparing the first characters in either binary variant, so that "aa" and "ab" (in binary) ended up comparing as equal. I'm not sure what to say about it working now -- I probably fail at testing.

Edited by -Ultima-

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

Share this post


Link to post
Share on other sites

You can submit to Trac anonymously :D

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