FileRead & binary data Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
newbieforever
Posts: 493
Joined: 24 Aug 2016, 03:34

FileRead & binary data

22 May 2018, 01:30

I would like to use FileRead, Str, c* MyFile.exeto read the content of an exe file, and then replace all binary null characters by @ to have an readable Str which I can use for further processing. How this replacing could be done?
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: FileRead & binary data

22 May 2018, 23:56

- Some ideas are:
- Loop through each byte using NumGet, use NumPut on any null bytes.
- Binary data to hex string, edit hex string (use RegEx to put a separator character between every pair of hex digits, replace any 00s, remove the separator character), hex string to binary data.
- Write a machine code function to search for null characters and replace them using NumPut.
InBuf function currently 32-bit only (machine code binary buffer searching) - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=28393
- Write a machine code function to replace null characters with a specified character.
- (Then, for all of the approaches, to finish, use StrGet to retrieve an ANSI string.)

- Here's an example to loop through each byte using NumGet, and use NumPut on any null bytes.

Code: Select all

q:: ;read binary file, replace null bytes with a character
vChar := "@"
vPath := A_AhkPath

;AHK v1
;FileGetSize, vSize, % vPath
;FileRead, vData, % "*c " vPath

;AHK v1/v2 two-way compatible
oFile := FileOpen(vPath, "r")
oFile.Pos := 0
oFile.RawRead(vData, vSize := oFile.Length)
oFile.Close()

vOrd := Ord(vChar)
Loop, % vSize
	if !NumGet(&vData, A_Index-1, "UChar")
		NumPut(vOrd, &vData, A_Index-1, "UChar")
Clipboard := StrGet(&vData, vSize, "CP0")
MsgBox, % "done"
return
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: FileRead & binary data

23 May 2018, 00:20

newbieforever wrote:I would like to use FileRead, Str, c* MyFile.exeto read the content of an exe file, and then replace all binary null characters by @ to have an readable Str which I can use for further processing. How this replacing could be done?
I think it might be worth mentioning that you wont get a working string after you have done this.
Depending on your encoding you still need to do several different things.
I would not reccomend doing this.
Recommends AHK Studio
newbieforever
Posts: 493
Joined: 24 Aug 2016, 03:34

Re: FileRead & binary data  Topic is solved

23 May 2018, 01:22

I am using now this:

Code: Select all

; Purpose: Read binary data from a file (e.g. an exe file) and convert it 
; to a readable string which can be processed with AHK string commands. 
; To enable this the binary null is replaced by a character.
; The string can be used e.g. to view/explore/extract program resources.

NullRepl := "5F"		; Replace binary null by _

FileSelectFile, File, , %A_ScriptDir%, Select a binary file, *.exe

Gui Wait: Show, w500 h0, Just a second ...
VarCap := VarSetCapacity(Content,0)
FileRead Content, %File%
VarCap := VarSetCapacity(Content)
SetFormat Integer, H

Loop %VarCap%
  {
    If (Mod(A_Index, 2) <> 0)
      {
        Byte := SubStr(NumGet(Content, A_Index-1, "UChar"), 3) " "
        If (Byte = 0x0)
            Byte := NullRepl
        String .= Chr("0x" . Byte)
      }
  }

FileAppend, %String%, %A_ScriptDir%\String.txt

ExitApp
b0dhimind
Posts: 18
Joined: 20 May 2015, 11:41

Re: FileRead & binary data

20 Sep 2022, 09:54

Is there a version of this that works at a string level instead of byte level?
I copied rich text from OneNote, which is basically binary when we load ClipboardAll to a file, and want to do some mass replace based on the strings searched and then have it automatically load back to the clipboard for pasting.

I did see some things related to Binread but couldn't make sense of it and not sure if it would work with string.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Archimede, Draken, haomingchen1998, LepG, ReyAHK and 262 guests