Literally write hex (binary) to a file Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
User avatar
Exaskryz
Posts: 2882
Joined: 17 Oct 2015, 20:28

Literally write hex (binary) to a file

18 Feb 2024, 21:20

Struggling on this one. Can't find any examples for whatever reason. Been a while since I played with AHK. Open to v1 or v2 solutions (just installed v2).

Code: Select all

SetFormat, Integer, Hex
string:=5041544348 ; sorry misleading name, it's really just supposed to be a literal raw value represented in hex
MsgBox % string
myfile:=FileOpen("Test.bin", 0x1) ; edit, oops, meant to have 0x1 here to always overwrite the test file
myfile.RawWrite(string,10)
return
---------------------------
writefiletesting.ahk
---------------------------
0x12C7FDC9C
---------------------------
OK
---------------------------
What it's outputting to the test.bin file is
30 00 78 00 35 00 30 00 34 00
As viewed in a hex editor. That is interpreted as 0.x.5.0.4. ... not what I want.

What I want, of course, is it to have the hex editor open the file and show me the original value 5041544348

What's gone wrong is AHK assumes my string as is in the .ahk file is in decimal, when it's literally hex. So, I would have thought using string:=0x5041544348 would fix that? It does make the MsgBox say 0x50415444348 but it doesn't change the test.bin output.

I feel like it should be super obvious. Maybe once I ever stumble across an example, it will jive with me. All the past hour of search results get me a mix of answers from 15-5 years ago with far more complex functions than I would have thought necessary.

I wanted to use AHK to whip up a GUI to craft together bits and pieces of a hex file. But I may turn to just finding some linux terminal solutions. It's really easy in such a terminal to write out the hex I want, just not as pleasant to go through all the options in crafting that output. Old notes I have say it should be as simple as echo "50 41 54 ..." | xxd -r -p - AnyName.bin.

Hypothetically I could at least still use AHK to whip up the string of hex values, save to a .txt file, and open up the text file and use echo upon the text file. Cumbersome though to reboot the computer to flip OSes, hoped I could get everything done in just Windows. (Well, I can run a virtual machine I suppose, still a hassle.)
Last edited by Exaskryz on 19 Feb 2024, 10:34, edited 1 time in total.
iseahound
Posts: 1459
Joined: 13 Aug 2016, 21:04
Contact:

Re: Literally write hex (binary) to a file

18 Feb 2024, 21:34

Something like this?

Code: Select all

assembly := "
   ( Join`s Comments
   48 89 4c 24 08                 ; mov [rsp+8], rcx
   48 89 54 24 10                 ; mov [rsp+16], rdx
   4c 89 44 24 18                 ; mov [rsp+24], r8
   4c 89 4c 24 20                 ; mov [rsp+32], r9
   48 83 ec 28                    ; sub rsp, 40
   49 b9 00 00 00 00 00 00 00 00  ; mov r9, .. (lParam)
   49 b8 00 00 00 00 00 00 00 00  ; mov r8, .. (wParam)
   ba 00 00 00 00                 ; mov edx, .. (uMsg)
   b9 00 00 00 00                 ; mov ecx, .. (hwnd)
   48 b8 00 00 00 00 00 00 00 00  ; mov rax, .. (SendMessageW)
   ff d0                          ; call rax
   48 83 c4 28                    ; add rsp, 40
   c3                             ; ret
   )"
DllCall("crypt32\CryptStringToBinary", "str", assembly, "uint", 0, "uint", 0x4, "ptr", pcb, "uint*", 71, "ptr", 0, "ptr", 0)
just me
Posts: 9552
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Literally write hex (binary) to a file  Topic is solved

19 Feb 2024, 07:01

or

Code: Select all

#Requires AutoHotkey v2.0
; "PATCH" as ANSI string
Hex := "5041544348" ; sorry misleading name, it's really just supposed to be a literal raw value represented in hex
Bin := Buffer(StrLen(Hex) // 2, 0)
P := 0
Loop Parse, Hex {
   If (A_Index & 1)
      H := A_LoopField
   Else
      NumPut("UChar", "0x" . H . A_LoopField, Bin, P++)
}
Bin.Size := P
; now you can call File.RawWrite(Bin)
Str := ""
Loop Bin.Size
   Str .= Format("{:02X}" , NumGet(Bin, A_Index - 1, "UChar"))
MsgBox(Str)
User avatar
Exaskryz
Posts: 2882
Joined: 17 Oct 2015, 20:28

Re: Literally write hex (binary) to a file

19 Feb 2024, 10:37

In a couple tests this AM, that works greatly and adapted it easily fro the file writing, which indeed is what I am seeing in my hex editor. Looks like I just had to approach it as one character at a time rather than trying to do the whole string at once. So thanks for that insight!

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: No registered users and 31 guests