Search code examples
c#textboxbinarywriter

c# write binary from textbox


How can i write binary that was changed from textbox input. I have a decimal to binary converter but WriteByte does not allow me to use textbox. it gives me an error that i don't know what it means.

instead 0xFF. i want to put custom values 0 to 255. it must be something with 0x(modified value from textbox). Here is the code.

Stream outStream = File.Open(ofd.FileName, FileMode.Open);
outStream.Seek(0x6354C, SeekOrigin.Begin);
outStream.WriteByte(0xFF);

I don't care if someone give me negative rep and positive rep. i just need a help with binary writer


Solution

  • You would need to convert the texbox text to a byte value:

    var byteValue = Convert.ToByte(textBox.Text);
    outStream.WriteByte(byteValue);
    

    The method above converts a string like "255" to its corresponding byte value. If you want the input string to be in hex format like "0x8D", then you should use this overload instead:

     Convert.ToByte(textBox.Text, 16)
    

    Of course you should look into doing more checks to make sure input text is in a correct format, not null, etc.