title: Writing to a binary file between an offset range
body: <p><a href="https://i.stack.imgur.com/uga7T.png" rel="nofollow noreferrer">Image 1</a></p>
  
  <p>I have coded an application to read all the bytes from a binary file within an offset range, in this case between offset 0x0000 AND 0x0003F
  and I got the code correct.</p>
  
  <p>I'm finding it difficult to write the code to take a value from the user. I need the code to take a value from the text box and replace all bytes between a specific offset range. For example, I type <code>"Rock"</code> inside the text box, so I need the program to write <code>'R'</code> at offset 0x000 <code>'O'</code> at offset 0X001 <code>'C'</code> at Offset 0X002 and so on when I hit write to file option.</p>
  
  <p>This is the code I used to read from the binary file</p>
  
  <pre class="lang-cs prettyprint-override"><code>OFD.ShowDialog();
  
  BinaryReader br = new BinaryReader(File.OpenRead(OFD.FileName));
  
  for (int i = 0X00000; i &lt;= 0x0003F; i++)
  {
  
      br.BaseStream.Position = i;
  
      yobj += br.ReadByte().ToString("X2") + " ";
  
      rtb1.Text = yobj;
  
  }
  </code></pre>
  
  <p>I managed to write the code in order to write each byte between a specific offset range, but the problem is it's writing the ASCII value of the first alphabet from the text box. I need the program to write the ASCII of each alphabet. I have provided the code below.</p>
  
  <p><a href="https://i.stack.imgur.com/GWfUU.png" rel="nofollow noreferrer">Image 2:Writing to File</a></p>
  
  <p>UPDATE- I managed to make the correct code.</p>
  
  <p><a href="https://i.stack.imgur.com/xDUCZ.png" rel="nofollow noreferrer">Image 3:Writing to File Correct Code</a></p>
  
  <pre><code>public void btnwrite_Click(object sender, EventArgs e)
  {
      OFD.ShowDialog();
  
      BinaryWriter bw = new BinaryWriter(File.OpenWrite(OFD.FileName));
      byte[] bytes = Encoding.ASCII.GetBytes(inp.Text);
      int[] result = Array.ConvertAll(bytes, Convert.ToInt32);
  
      for (int i= 0x0000;i&lt;bytes.Length;i++)
      {
          bw.Seek(i, SeekOrigin.Begin);
          bw.Write(result[i]);
      }
  
      bw.Close();
  }
  </code></pre>