BinaryWriter.Write(byte value) issue when value > 127 [closed]
Asked Answered
S

1

7

I'm writing raw byte values to a file:

  • When values are <= 127, everything is ok.
  • But if a byte is > 127, it gets all messsed up.

I've already tried changing encoding format and such, with no success.

public static void Generate()
{
    var fileName = "Test.bin";

    if (File.Exists(fileName))
        File.Delete(fileName);

    using (var binaryWriter = new BinaryWriter(File.Open(fileName, FileMode.Create)))
    {
        var byteArray = new byte[] {0x01, 0x02, 0x7F, 0x80};

        foreach (var b in byteArray)
        {
            binaryWriter.Write(b);
        }
    }
}

On the above code, the resulting file should be:

01 02 7F 80

But what I get is:

01 02 7F D0 90

Any clue on whats happening?

Here's a test application: http://pastebin.com/0Cfv3Snc

Here's the generated files: http://postimg.org/image/55un9lar1/

I doesn't work on any of my two PCs. Running .NET 4.0 on Windows 10.

Stevenson answered 25/11, 2015 at 13:34 Comment(16)
BinaryWriter has an overload to write an array to the file, have you tried just using binaryWriter.Write(byteArray) and skipping the loop?Yclept
Yes, I've tried that too.Stevenson
I'm unable to reproduce, a copy/paste of your code produces your expected file on my system.Yclept
That's strange... I've had this problem before, and it apparently solved itself after I had given up. Now I'm writing a new .dll, and the problem is here again. Could you please send me the complete code you used to test it? I don't know if there could be an issue on my PC, or my .NET.Stevenson
Here it is: pastebin.com/CHcqjGzCYclept
Although C#'s byte struct is by default unsigned, many things use signed bytes which would only go up to 127. Are you sure whatever program you are using to read that file is not the issue?Microscopic
Here is the resulting file (you can click/drag files into VS to get a hex editor view of the file): postimg.org/image/g7c0okfl1Yclept
@RogueCSDev Assuming he's using a hex editor, it shouldn't be adding bytes to represent something that isn't there. Bytes are bytes when written to a binary file, they don't have signs, they are just single bytes.Yclept
In fact, I'm using Notepad++, in Hex mode. I've tested the code @RonBeyer used, on another computer, then it worked. I moved the method to another location, it worked. Then I tried again, it's all wrong again. It seems this issue is somewhat random, but I can't narrow down the cause.Stevenson
After using this array: var byteArray = new byte[] { 0x00, 0x00, 0x50, 0x00, 0x30, 0x00, 0x02, 0x00, 0x20, 0x00, 0x00, 0x01, 0x00, 0x01, 0xFE, 0x02, 0xFE, 0xA0, 0xA1, 0x00, 0x00, 0x7B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; The issue appears again, and stops working forever..Stevenson
Are you sure you are using the BinaryWriter class from System.IO which is defined in mscorlib? It might be that you are using another BinaryWriter or that you are calling an extension method which is using another encoding, e.g. a 7-bit encoding such as it is used by BinaryWriter.Write7BitEncodedIntQueri
P.S.: Check the using directives at the top of the file to see whether there is anything suspicious, comment them out and see what namespaces VS wants to add. System.IO should be the one you need. Anyway, for troubleshooting it might help if you could create a small command line program that reproduces your problem.Queri
@DirkVollmar I've checked the using directives, it's all good, using System.IO. Here's a test program: pastebin.com/0Cfv3SncStevenson
I've tested on 2 systems here, the problem persists. My files: postimg.org/image/55un9lar1 I can't imagine what could be causing this, I don't understand how can the same code work on one system and not on another. Any ideas?Stevenson
You are chasing the wrong problem. The issue here is Notepad++, a text editor. Like all text editors, it expects to read a text file. And has to guess what the encoding of the file might be. It flounders at it, this is not text. Use a decent hex viewer instead, like HxD.Anatol
Wow, I'd never suspect Notepad++ was the issue. I trusted its Hex Editor was good. I used HxD and the data is indeed good. Thanks a lot!Stevenson
S
0

Solved, there was never any issue.

Notepad++ Hex Editor was the problem, it was show incorrect data (don't know why, though, I expected for it to simply show raw data).

Stevenson answered 25/11, 2015 at 17:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.