-1

Problem that I am having is :

I am reading a array of bytes using binary reader to construct a windows gui for editing of a file, for instance I am reading the following byte array: 123cbb75

//read the first 4 bytes from 0x36A2
byte[] buffer = br.ReadBytes(4);

Array.Reverse(buffer);

Encoding enc8 = Encoding.UTF8;
data = enc8.GetString(buffer);

data = (BitConverter.ToInt32(buffer, 0).ToString("X8"));
textBox1.Text = data; //reads the full string of hex
                      //values 123CBB75-- first 4 bytes
textBox2.Text = data.Substring(0, 2);//reads 12

textBox3.Text = data.Substring(2, 1);//reads 3
textBox4.Text = data.Substring(3 ,1);//reads C
textBox5.Text = data.Substring(4, 1);//reads B
textBox6.Text = data.Substring(5, 1);//reads B
textBox7.Text = data.Substring(6, 1);//reads 7
textBox8.Text = data.Substring(7, 1);//reads 5
br.Dispose();

I can easily edit the first byte because it has a two integer representation using Convert.ToByte (to edit a string from a text box and write it back into the binary file)

Question is : How do I edit the singular values from the text box so then it can write the value correctly back using binarywriter such as from textbox 3 or 4. I cannot use Convert.ToByte because its a singular numerical or alphabetical figure.

bw.Write(Convert.ToByte(textBox3.Text, 16)); //output is not correct when writing the data back to the file, if I want to change 3 to 5.

zwcloud
  • 4,546
  • 3
  • 40
  • 69
celtics33
  • 15
  • 6

1 Answers1

0

I am not sure what you are trying to do. But since your textbox has a single character string, you can do this:

bw.Write(Convert.ToByte(textBox3.Text[0], 16));

Here is more code that writes bytes to a file and then reads them back:

var a = Convert.ToByte('a');
var one = Convert.ToByte("1", 16); // If you want to provide the base

using (var writer = new BinaryWriter(File.Open("Path", FileMode.Create)))
{
    writer.Write(a);
    writer.Write(one);
    writer.Write("X"[0]);
}

using (var reader = new BinaryReader(File.Open("Path", FileMode.Open)))
{
    var aa = (char)reader.ReadByte();
    Console.WriteLine(aa);
    var oneOne = (char)reader.ReadByte();
    var x = (char)reader.ReadByte();
}
CodingYoshi
  • 25,467
  • 4
  • 62
  • 64
  • I am trying to make an application where the end user can edit a ROM file....so if the user selects certain criteria via a GUI, the following 7 text boxes will populate with data that are converted from byte to string so it can be displayed in the text boxes. So if the end user wants to edit the player num that is easy to edit, just edit the 12 to 15 via the text box, however the attribute such as quickness is represent by only a B, the second value of the byte represents another attribute. I am not sure how to edit the one value because Convert.ToByte does not work correctly with one value. – celtics33 Mar 08 '18 at 21:35
  • @celtics33 what do you mean it does not work? I have put many examples in my answer. Did you try it? – CodingYoshi Mar 08 '18 at 23:15