I have a byte array like so
var byteArray = new byte[]
{
0x9C, 0x50, 0x53, 0x51, 0x52, 0x41, 0x50, 0x41, 0x51, 0x41, 0x52, 0x41, 0x53,
0x48, 0x83, 0xEC, 0x28,
0x48, 0xB9, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Line 3
0x48, 0xB8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Line 4
0xFF, 0xD0,
0x48, 0x83, 0xC4, 0x28,
0x41, 0x5B, 0x41, 0x5A, 0x41, 0x59, 0x41, 0x58, 0x5A, 0x59, 0x5B, 0x58,0x9D,
0xC3
};
I want to replace the following bytes on line 3
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
As well as the following bytes on line 4
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
With different bytes (other than 0x00)
Note that the bytes I want to change on line 3 are different to the bytes I want to change on line 4
What is the simplest way to accomplish this?
If you don't want / can't modify your source array, you can easily create another one with same values :
byte[] copyArray;
byteArray.CopyTo(copyArray, 0);
// or
copyArray = byteArray.ToArray() // following @Matthew Watson
Then if you want to change your eigth last values for line 3 :
byte[] ReplaceThirdLineValues(byte[] source, params byte[] newValues)
{
byte[] copyArray;
byteArray.CopyTo(source, 0);
for (int i = 0 ; i < newValues.Length && i < 8 ; i++)
// i < 8 because in your array there are 8 0x00 in a row
if (copyArray[19 + i] == 0x00 && newValues[i] > 0x00)
// newValues[i] > 0x00, so if you do not want to override value,
// just give 0x00 to your parameter
copyArray[19 + i] = newValues[i];
}
For your fourth line, replace copyArray[19 + i]
by copyArray[29 + i]
byte[] copyArray = ReplaceThirdLineValues(byteArray, 0x01, 0x02, 0x03, 0x04);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.