Hex subtraction script

Hi to all
I’m very new to this and i know nothing about writing scripts
I want to make a script which acts like XorSelectionHex script but instedd of xor, subtract two first selected bytes from starting value. sum minus next two etc
The script instead of asking the input xor key should ask the starting value for subtraction.
Any help would be appreciated.

fe selected bytes are 00 00 69 89 00 0B
starting value 55AA
55AA - 0000 = 55AA
55AA - 6989 = FFFF FFFF FFFF EC21 → EC21
EC21 - 000B = EC16

A script something like this work:

// get start and size
int64 size = GetSelSize();
int64 start = GetSelStart();
if (size == 0) {
    MessageBox( idOk, GetScriptName(), "Please select bytes before running this script." );
    return -1;
}

// input hex key string
string sKey = InputString( GetScriptName(), "Input the XOR hex key.", "55AA");
if (sKey == "") {
    MessageBox(idOk, GetScriptName, "Please enter a valid hex key.");
    return -1;
}
ushort value;
SScanf(sKey, "%x", value);

// modify the data
int64 i;
for( i = 0; i < size-1; i+=2 )
{
    value = value - ReadUShort( start+i );
    WriteUShort( start+i, value );
}

Make sure your file is in Big Endian mode using ‘View > Endian’ for this to work correctly.

Graeme
SweetScape Software

1 Like

Worked perfect!!!
Thank you very much!

The script is doing subtraction on the line ‘value = value - ReadUShort( start+i );’. If you run this on ‘00 00 69 89 00 0B’ you should get ‘55 AA EC 21 EC 16’ if in big endian mode.

Graeme
SweetScape Software

Yes sorry my mistake!
Edited my post! Thank you again!