how can i update an array with new values? C#

62 views Asked by At

Main question is how can i update the values in array?like im getting only 4 bytes instead of all cause its integer.

byte[] iarray = File.ReadAllBytes(path);
uint t;


for (int i = 61; i < 3440; i++)
{
    i+=3;
    t = BitConverter.ToUInt32(iarray,i);
  
    
    if (t > 0)
    {
        t += 96;
    }
    iarray = BitConverter.GetBytes(t);
}   


    File.WriteAllBytes(path,iarray);
2

There are 2 answers

0
Serg On

You are trying to rewrite the whole array instead of updating the only portion of data (4 bytes) inside the existing array. So, after you got the final value of the t, you can do the following to place this new value back into the array instead of the old value (so, use the same value of 'i' for position)

BitConverter.GetBytes(t).CopyTo(iarray, i);
0
RandomUser On
public void temp(string path)
  {
     byte[] iarray = File.ReadAllBytes(path);
     uint t;


     for (int i = 61; i < 3440; i+=4)
     {
        t = BitConverter.ToUInt32(iarray, i);


        if (t > 0)
        {
           t += 96;
        }
        var updatedArray = BitConverter.GetBytes(t);
        updatedArray.CopyTo(iarray, i);
     }


     File.WriteAllBytes(path, iarray);
  }