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")
}