Issue with replacing HEX values

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
Scr1pter
Posts: 1272
Joined: 06 Aug 2017, 08:21
Location: Germany

Issue with replacing HEX values

14 Apr 2024, 14:03

Hello,

I'm trying to replace HEX values in a MIDI file.
I used jNizM's great str2hex and hex2str functions.
Then I created two arrays: One with the search values and the other one with the replace values.

The script runs, but the output file is only 4 byte - the orig file was 1 kb.
Any idea what the problem could be?
FileRead, content, *c C:\1.mid did not work either, which is understandable.

Thanks for any help and best regards!

Code: Select all

F1::
FileRead, content, C:\1.mid
hexValue     := StringZuHex(content)
searchArray  := ["90\s+76", "90\s+74", "90\s+72", "90\s+6F"]
replaceArray := ["91 76", "91 74", "91 72", "91 6F"]

for index, search in searchArray
{
  replace  := replaceArray[index]
  hexValue := RegExReplace(hexValue, search, replace)
}
stringValue := HexZuString(hexValue)
FileAppend, %stringValue%, C:\1_new.mid
MsgBox, finished
return

StringZuHex(string)
{
  VarSetCapacity(bin, StrPut(string, "UTF-8")) && len := StrPut(string, &bin, "UTF-8") - 1 
  if !(DllCall("crypt32\CryptBinaryToString", "ptr", &bin, "uint", len, "uint", 0x4, "ptr", 0, "uint*", size))
  {
    throw Exception("CryptBinaryToString failed", -1)
  }
  VarSetCapacity(buf, size << 1, 0)
  if !(DllCall("crypt32\CryptBinaryToString", "ptr", &bin, "uint", len, "uint", 0x4, "ptr", &buf, "uint*", size))
  {
    throw Exception("CryptBinaryToString failed", -1)
  }    
  return StrGet(&buf)
}
HexZuString(string)
{
  if !(DllCall("crypt32\CryptStringToBinary", "ptr", &string, "uint", 0, "uint", 0x4, "ptr", 0, "uint*", size, "ptr", 0, "ptr", 0))
  {
    throw Exception("CryptStringToBinary failed", -1)
  }
  VarSetCapacity(buf, size, 0)
  if !(DllCall("crypt32\CryptStringToBinary", "ptr", &string, "uint", 0, "uint", 0x4, "ptr", &buf, "uint*", size, "ptr", 0, "ptr", 0))
  {
    throw Exception("CryptStringToBinary failed", -1)
  }
  return StrGet(&buf, size, "UTF-8")
}
Please use [code][/code] when posting code!
Keyboard: Logitech G PRO - Mouse: Logitech G502 LS - OS: Windows 10 Pro 64 Bit - AHK version: 1.1.33.09

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: ArkuS, Spawnova, TAC109 and 210 guests