AnyOne2 Posted January 12, 2017 Share Posted January 12, 2017 (edited) I extracted some binary data using the function BinaryMid. Now I want to modify some of those byte before interpreting the whole binary sequence as UTF-16LE string. How to do this? Note: searching for UDFs I found one called Binary.UDF (by Ward), but I seems not to be available anymore; the function "_BinaryPoke" would have been very helpful I think... Edited January 16, 2017 by AnyOne2 Link to comment Share on other sites More sharing options...
anthonyjr2 Posted January 12, 2017 Share Posted January 12, 2017 I found a copy of the UDF on Github: https://github.com/fediaFedia/Omnimo/blob/master/AutoIT/Includes/Binary.au3 AnyOne2 1 UHJvZmVzc2lvbmFsIENvbXB1dGVyZXI= Link to comment Share on other sites More sharing options...
AnyOne2 Posted January 12, 2017 Author Share Posted January 12, 2017 Thanks for the quick response! I'll have a look if this really fits my needs Link to comment Share on other sites More sharing options...
AnyOne2 Posted January 13, 2017 Author Share Posted January 13, 2017 OK, here's my conclusion after some tries with this Binary.au3: the manipulations of Binaries using DLLStructs are incredible slow. The compiled AutoIt script needs about 100 seconds, using PHP the same algorithm finishes after just 2 seconds. So why using AutoIt? Because I need to be able to compile the algorithm and distribute it as self-contained executable... Any hints? Link to comment Share on other sites More sharing options...
AndyG Posted January 13, 2017 Share Posted January 13, 2017 9 hours ago, AnyOne2 said: the manipulations of Binaries using DLLStructs are incredible slow. The compiled AutoIt script needs about 100 seconds, using PHP the same algorithm finishes after just 2 seconds. Manipulating of Binaries using DLLStructs is incredibly FAST (if you know what to do!) Without a Script to verify your statement, any other discussion is useless...please show a little example script. Link to comment Share on other sites More sharing options...
AnyOne2 Posted January 13, 2017 Author Share Posted January 13, 2017 (edited) 19 hours ago, AndyG said: Manipulating of Binaries using DLLStructs is incredibly FAST (if you know what to do!) You're right, this was probably a hasty conclusion, I'm sorry. So here's a simplified version of my code to show you what I'm doing in my script. I would really appreciate it if someone could tell me why it runs so slow. expandcollapse popup; open input file $hFileIn = FileOpen($sFilePath, $FO_READ + $FO_BINARY) $dBinary = FileRead($hFileIn) FileClose($hFileIn) $iBinLen = BinaryLen($dBinary) ; open output file $sFileOut = $sFilePath & '.txt' $hFileOut = FileOpen($sFileOut, $FO_OVERWRITE) $iOffset = 4 Do ; extracting length $iLenStr = Int(BinaryMid($dBinary, $iOffset+8, 1)) + (256 * Int(BinaryMid($dBinary, $iOffset+9, 1))) ; get raw text $dRawStr = BinaryMid($dBinary, $iOffset, $iLenStr) $iOffset += $iLenStr + 10 ; adapt and convert text $iVal = Mod(iLenStr, 32) ; added/updated adapt($dRawStr, $iVal); $sText = BinaryToString($dRawStr, 2) ; 2=UTF16LE ; write output FileWriteLine($hFileOut, $sText) $iCnt += 1 If $iCnt >= 500 Then ConsoleWrite('*') $iCnt = 0 EndIf Until $iOffset >= $iBinLen FileClose($hFileOut) ;------- INTERNAL FUNCTION ------------------------ Func adapt(ByRef $dBinary, $iVal) ; create C struct from binary variable Local $dBuffer = DllStructCreate("byte[" & (BinaryLen($dBinary)) & "]") DllStructSetData($dBuffer, 1, $dBinary) ; loop through structure and add val to all bytes For $i=1 To BinaryLen($dBinary) Step 1 $iNewValue = DllStructGetData($dBuffer, 1, $i) + $iVal DllStructSetData($dBuffer, 1, $iNewValue, $i); Next ; write result back to binary variable $dBinary = DllStructGetData($dBuffer, 1) EndFunc  Edited January 14, 2017 by AnyOne2 Link to comment Share on other sites More sharing options...
AndyG Posted January 14, 2017 Share Posted January 14, 2017 Hi, do you have an example file to convert and a value for $iVal? I think you have been caught by the trap called XY-Problem. The goal is to add a (always the same?) value to some bytes within a file?! The job should be done within some milliseconds... Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted January 14, 2017 Moderators Share Posted January 14, 2017 AndyG, Love the link! M23  Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area  Link to comment Share on other sites More sharing options...
AnyOne2 Posted January 14, 2017 Author Share Posted January 14, 2017 (edited) You're right again, I forgot to include the following line (I now updated my initial post): $iVal = Mod($iStrLen,32) it shows that the value of $iVal changes for each loop iteration (between 0 and 31). Another info: the input files have sizes between 5 and 10 MB, each loop iteration handles about 250 bytes (to average). My PHP script takes 2260 ms for a file of about 6.5 MB (Win10, Intel i7-4900MQ 2.8 GHz, 16GB RAM, 500 GB SSD). Thank you for bearing with me! Edited January 14, 2017 by AnyOne2 Link to comment Share on other sites More sharing options...
AnyOne2 Posted January 15, 2017 Author Share Posted January 15, 2017 (edited) I solved my speed problems, the script now finishes within 6-8 seconds, that's acceptable for me. What did I do? Instead of reading the file as binary, I read it as ANSI string and used StringToASCIIArray to get an array of integers in the range of 0-255. Like this I could do all my manipulations on the array and I made the byte-swap (to read UTF16LE) myself before calling StringFromASCIIArray. The conversion to an array takes about 2 seconds, 4-6 seconds are needed to process the file (7MB). I'm happy Thanks for your replies anyway! Edited January 15, 2017 by AnyOne2 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