tags:

views:

69

answers:

3

How do I use WriteByte? The below code will not work, coming up with both a CS1502 error, and a CS1502 error at the "stream.WriteByte(totalSeasons);" line. It's frustrating because I am incredibly close to finishing this program.

Essentially, my problem is that I need to write binary data to a file on the click of a button. I can just write this for example: stream.WriteByte(0xCD); The problem is that I need to allow the user to type in the data they want to be written into a textbox, and then on the click of a button the data will be converted from decimal to hex, and then put in. I think I've figured the conversion part out, but I can't edit the binary with the data because it seems that WriteByte can only write Hex values like 0xCD, and there seems to be no way to store the hex value and still write it. I've tried stream.WriteByte(ByteName); but that comes with the errors described above.

void BtnTotalSeasonsClick(object sender, EventArgs e)
{
    using (var stream = new FileStream(drvFile, FileMode.Open, FileAccess.ReadWrite))  
    { 
        System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
        Byte[] totalSeasons = enc.GetBytes(txtTotalSeasons.Text);
        stream.Position = 4; 
        stream.WriteByte(totalSeasons);
    } 
}
+1  A: 
Alex Humphrey
Why would you want to maximize the number of I/O calls?
Ben Voigt
Good point Ben, it's getting pretty late here in the UK :)
Alex Humphrey
@Ben: maximize? Please... It's not hard to imagine how to write 1 bit at a time...
codekaizen
... if you had a file API that let you address individual bits. I don't.
Ben Voigt
+3  A: 

totalSeasons is more than one byte, so you'd call the Write method which writes an entire block of bytes.

Is txtTotalSeasons.Text supposed to turn into a single byte? Maybe you're looking for Byte.TryParse instead of Encoding.GetBytes.

EDIT: Provide the entire error message, not just the number. Also, what exactly is in the text box. You seem to be confused about hex vs decimal vs binary vs ASCII. They are different string representations of a number.

0xCD is not a hex value. It is a C#-conformant hexadecimal representation of a number. (Another hexadecimal representation, not allowed by C#, is CDh). The compiler turns it into a number in the .NET internal format (which happens to be binary, but this isn't important). Then the WriteByte or Write call stores the number into the file in binary. You'd get the same result from stream.WriteByte(0xCD), stream.WriteByte(206), or stream.WriteByte(0316) since the compiler converts all of them to the same internal representation. If you want to write a variable then there's no reason to use hex at all, you need the internal representation which WriteByte expects.

You said that the user enters a number between 0 and 255, in decimal? Then the conversion required is from a decimal string representation to the internal representation, this is called "parsing".

Try this code:

stream.WriteByte(Byte.Parse(txtTotalSeasons.Text));
Ben Voigt
Thank you, but I have another issue. Now error CS1501 comes up. How do I write Write in this context?
JC Leyba
Never mind. Fixed it on my own.
JC Leyba
A: 

Consider using a BinaryWriter.

Shimmy