Jump to content

Splitting a File to Byte Array


KFUPM
 Share

Recommended Posts

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.

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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 by mikell
Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...