-1

I have been trying to make a test program, that writes a byte from a textbox to an offset in another textbox.

I have been trying to do that:

using (BinaryWriter bw = new BinaryWriter(File.OpenWrite(ofd.FileName)))
{
     bw.Seek(toolStripTextBox1.Text, SeekOrigin.Begin);
     bw.Write((byte)textBox1.Text);
}    

toolStripTextBox1 contains the offset where I want to write and textBox1 contains the bytes I want to write.

Let's say I typed in the toolStripTextBox1 "0xF450B0" and in textBox1 "1052", I wanted it to write the location at "0xF450B0" to this: http://gyazo.com/49afd54dfc54fc15be47a7e08f300960

Dmitry
  • 13,797
  • 6
  • 32
  • 48
Gal
  • 1
  • 1
  • Is it a hacking attempt? – Dmitry Sep 08 '14 at 19:34
  • Nope, not a hacking attempt since I don't hack. I want to write Hexadecimal data into a ROM, if you don't know what ROM is, you better search in google. – Gal Sep 10 '14 at 04:47

1 Answers1

1

You can convert the offset (hex) to an integer if that is always what the user enters. That looks like:

using (BinaryWriter bw = new BinaryWriter(File.OpenWrite(ofd.FileName)))
{
     bw.Seek(Convert.ToInt32(toolStripTextBox1.Text, 16), SeekOrigin.Begin);
     bw.Write((byte)textBox1.Text);
}

Just to emphasize - if the user enters something that is not hexidecimal into toolStripTextBox1 you will get an exception. You could use Int32.TryParse if this is a scenario you need to handle. For more information on the conversion between hex and integer see this site.

Best of luck!

drew_w
  • 10,320
  • 4
  • 28
  • 49