开发者

WriteByte, how to use it

开发者 https://www.devze.com 2023-01-06 13:18 出处:网络
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

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, Ev开发者_开发问答entArgs 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);
    } 
}


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));


Have you tried:

foreach (var b in totalSeasons)
{
  stream.WriteByte(b);
}

WriteByte writes a single byte at a time, not an array of bytes.


Consider using a BinaryWriter.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号