bufferToHex for 2 Bytes

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
DevWithCoffee
Posts: 55
Joined: 13 Oct 2020, 12:16

bufferToHex for 2 Bytes

Post by DevWithCoffee » 10 Jan 2024, 01:11

I was having a lot of difficulty extracting data from memory using the readString function, so through this solution created by RHCP I was wondering how to separate 2-byte data, so I decided to check if the String's position is even or odd to read without repeating any bytes.

Source: p297901

Function:

Code: Select all

bufferTo2BHex(byRef buffer, sizeBytes)
{
    loop % sizeBytes
	{
		if((A_Index&1)>0)
		{
			s .= Format("{:04X} ", NumGet(&buffer + 0, A_Index - 1, "UShort"))
		}
	}
    return rtrim(s, A_space)
}
Example of use:

Code: Select all

#SingleInstance Ignore
SetBatchLines, -1
SetKeyDelay, 0
SetWorkingDir %A_ScriptDir%
#Include <classMemory> ; On Lib folder

;Extras function
bufferTo2BHex(byRef buffer, sizeBytes)
{
    loop % sizeBytes
	{
		if((A_Index&1)>0)
		{
			s .= Format("{:04X} ", NumGet(&buffer + 0, A_Index - 1, "UShort"))
		}
	}
    return rtrim(s, A_space)
}

;App
Run, % "OldVidPlayer.exe", , , VidPlayerPID
WinWait, % "ahk_pid" VidPlayerPID,, 2 ; Wait 2 seconds
if(ErrorLevel > 0)
{
    ExitApp
}
Sleep 1000
return
ScreenCaptureToHex:
_w := 480
_h := 320
_bytes := (_w*_h)*2
mem := new _ClassMemory("ahk_pid " VidPlayerPID, "", hProcessCopy)
Hotkey IfWinActive, % "ahk_pid " VidPlayerPID
Hotkey, F12, StartSnapshot
_screen := mem.readRaw(mem.BaseAddress + 0x000A3BD4, myBuffer, _bytes, 0xDC, 0x48, 0x2C, 0)
_hx := bufferTo2BHex(myBuffer, _bytes)
FileDelete, _result.txt
FileAppend, % _hx , _result.txt
return
Example Output:
8000 0400 8400 0010 8010 0410 C618 C6F8 A65E 4100 6100 8100 A100 C100 E100 200 2200 4200 6200 8200 A200 C200 E200 300 2300 4300 6300 8300 A300 C300 E300 (...)
Last edited by DevWithCoffee on 13 Jan 2024, 09:17, edited 3 times in total.

just me
Posts: 9556
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: bufferToHex for 2 Bytes

Post by just me » 10 Jan 2024, 04:33

Two bytes have four halfbytes / nibbles, so you should use

Code: Select all

s .= Format("{:04X} ", NumGet(&buffer + 0, A_Index - 1, "UShort"))

User avatar
DevWithCoffee
Posts: 55
Joined: 13 Oct 2020, 12:16

Re: bufferToHex for 2 Bytes

Post by DevWithCoffee » 10 Jan 2024, 07:30

What a lack of attention on my part, I was so happy and so tired when I finally discovered this solution that I didn't realize my mistake, thank you.

Post Reply

Return to “Scripts and Functions (v1)”