-Ultima- Posted April 18, 2008 Share Posted April 18, 2008 (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 /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 April 18, 2008 by -Ultima- [ WinINet.au3 | Array.au3 (Optimized) | _UnixTimeParse() ] Link to comment Share on other sites More sharing options...
Jango Posted April 18, 2008 Share Posted April 18, 2008 I don't expect too many people to find much use for this function, but I'll put it up anyway, just because /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. Link to comment Share on other sites More sharing options...
Siao Posted April 18, 2008 Share Posted April 18, 2008 (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 April 18, 2008 by Siao "be smart, drink your wine" Link to comment Share on other sites More sharing options...
-Ultima- Posted April 18, 2008 Author Share Posted April 18, 2008 (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 April 19, 2008 by -Ultima- [ WinINet.au3 | Array.au3 (Optimized) | _UnixTimeParse() ] Link to comment Share on other sites More sharing options...
NELyon Posted April 18, 2008 Share Posted April 18, 2008 You can submit to Trac anonymously Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now