KFUPM Posted September 27, 2015 Posted September 27, 2015 Hi, I have a binary file and I need to split its content to an array - or a structure - of bytes, do my work on it, then reassemble it again and write an output file.Example: File contains: "0x123456" Split to an array: [ 0x12 , 0x34 , 0x56 ] ; Work on the array Array becomes: [ 0xFE, 0xBD, 0x35] Write output file "0xFEBD35"Seemingly very simple task, yet it eludes me.
computergroove Posted September 27, 2015 Posted September 27, 2015 FileOpen(), FileReadToArray(), StringSplit, StringLeft etc, FileClose()Do you have any code for us to look at? Write out a few snippets of the file and the file name. Get Scite to add a popup when you use a 3rd party UDF -> http://www.autoitscript.com/autoit3/scite/docs/SciTE4AutoIt3/user-calltip-manager.html
KFUPM Posted September 27, 2015 Author Posted September 27, 2015 I tried all of these before, I have been trying to get this to work for a while now. FileReadToArray() just takes the whole file to an array with single string element, while StringSplit, when using "" as a delimiters, split the string by character - utf-8 char. with 3 bytes - not by byte. I just want a way to work purely with bytes instead of characters. The file is just a binary file with some utf-8 strings sprinkled through it. I need to extract these strings, fix the issues they have, and put them back.
computergroove Posted September 27, 2015 Posted September 27, 2015 Do I really need to ask again? I already asked. You say you you tried stuff and couldn't get it to work. Please post the code that didnt work and point out where it didnt work. Add a snippet of code that you want to read into your array or we cant debug it for you. Get Scite to add a popup when you use a 3rd party UDF -> http://www.autoitscript.com/autoit3/scite/docs/SciTE4AutoIt3/user-calltip-manager.html
Celtic88 Posted September 27, 2015 Posted September 27, 2015 Maybe that's what you are looking for?Local $sFileOpenDialog = FileOpenDialog("", @WorkingDir & "\", "Exe (*.exe)", 1) If @error Then Exit Local $hFileOpen = FileOpen($sFileOpenDialog, 16) If $hFileOpen = -1 Then MsgBox($MB_SYSTEMMODAL, "", "An error occurred when reading the file.") Exit EndIf Local $sFileRead = FileRead($hFileOpen) FileClose($hFileOpen) Local $File_struct = DllStructCreate("BYTE File[" & BinaryLen($sFileRead) & "]") DllStructSetData($File_struct, "File", $sFileRead) for $io = 0 to BinaryLen($sFileRead) msgbox(0,"","0x" & hex(DllStructGetData($File_struct, "File",$io),2)) next boludoz and KFUPM 1 1
mikell Posted September 27, 2015 Posted September 27, 2015 (edited) Or this using an array#Include <Array.au3> InetGet("https://www.autoitscript.com/forum/cdn/images/logo_autoit_210x72.png", @scriptdir & "\test.png") $file_in = @scriptdir & "\test.png" $file_out = "out.png" ; read $file = FileOpen($file_in, 16) $bytes = StringRegExp(FileRead($file), '[[:xdigit:]]{2}', 3) FileClose($file) _ArrayDisplay($bytes, "file") ; write $txt = "" For $i = 0 to UBound($bytes)-1 $txt &= $bytes[$i] Next $file2 = FileOpen($file_out, 18) FileWrite($file2, "0x" & $txt) FileClose($file2) ;#cs Dim $result[UBound($bytes)][2] For $i = 0 to UBound($bytes)-1 $result[$i][0] = $bytes[$i] $result[$i][1] = Chr("0x" & $bytes[$i]) Next _ArrayDisplay($result) ;#ce Edited September 27, 2015 by mikell KFUPM 1
jguinch Posted September 27, 2015 Posted September 27, 2015 On 9/27/2015 at 5:59 AM, KFUPM said: The file is just a binary file with some utf-8 strings sprinkled through it. I need to extract these strings, fix the issues they have, and put them back.I don't know which process you want to apply to each byte, but maybe it's possible do to it in one shot :#Include <Array.au3> $string = "0x123456" $bytes = Execute( StringRegExpReplace(StringReplace($string, "0x", "'0x'"), "([[:xdigit:]]{2})", " & _DoTheWork('$1')") ) ConsoleWrite($bytes) Func _DoTheWork($val) Return Hex(BitXOR(0xFF, Binary("0x" & $val)), 2) ; for example EndFunc KFUPM 1 Reveal hidden contents Network configuration UDF, _DirGetSizeByExtension, _UninstallList Firefox ConfigurationArray multi-dimensions, Printer Management UDF
Danyfirex Posted September 27, 2015 Posted September 27, 2015 just for fun.#include <Array.au3> Local $sFile='0x123456' Local $aArray[BinaryLen($sFile)] Local $n=0 For $i=3 to StringLen($sFile) step 2 ConsoleWrite( StringMid($sFile,$i,2) & @CRLF) $aArray[$n]=int('0x' & (StringMid($sFile,$i,2))) $n+=1 Next _ArrayDisplay($aArray) ;Convert Back Local $sOutput="0x" For $i=0 to UBound($aArray)-1 ConsoleWrite(hex($aArray[$i],2) & @CRLF) $sOutput&=hex($aArray[$i],2) Next ConsoleWrite($sOutput & @CRLF)Saludos KFUPM 1 Danysys.com AutoIt... Reveal hidden contents UDFs: VirusTotal API 2.0 UDF - libZPlay UDF - Apps: Guitar Tab Tester - VirusTotal Hash Checker Examples: Text-to-Speech ISpVoice Interface - Get installed applications - Enable/Disable Network connection PrintHookProc - WINTRUST - Mute Microphone Level - Get Connected NetWorks - Create NetWork Connection ShortCut
KFUPM Posted September 27, 2015 Author Posted September 27, 2015 On 9/27/2015 at 8:30 AM, Celtic88 said: Maybe that's what you are looking for?Local $sFileOpenDialog = FileOpenDialog("", @WorkingDir & "\", "Exe (*.exe)", 1) If @error Then Exit Local $hFileOpen = FileOpen($sFileOpenDialog, 16) If $hFileOpen = -1 Then MsgBox($MB_SYSTEMMODAL, "", "An error occurred when reading the file.") Exit EndIf Local $sFileRead = FileRead($hFileOpen) FileClose($hFileOpen) Local $File_struct = DllStructCreate("BYTE File[" & BinaryLen($sFileRead) & "]") DllStructSetData($File_struct, "File", $sFileRead) for $io = 0 to BinaryLen($sFileRead) msgbox(0,"","0x" & hex(DllStructGetData($File_struct, "File",$io),2)) next Oh my god, this looks almost identical to one of the ways I tried, yet this works while mine did not *bangs head on the wall*.I really should keep the failed scripts cause I'll never know now why it didn't work. This will haunt me for a couple of days. Thank you everyone, it both reads and writes fine now.
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