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.

Rafael Moura
  • 81
  • 1
  • 6
  • `BinaryWriter` has an overload to write an array to the file, have you tried just using `binaryWriter.Write(byteArray)` and skipping the loop? – Ron Beyer Nov 25 '15 at 13:39
  • Yes, I've tried that too. – Rafael Moura Nov 25 '15 at 13:39
  • 3
    I'm unable to reproduce, a copy/paste of your code produces your expected file on my system. – Ron Beyer Nov 25 '15 at 13:42
  • 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. – Rafael Moura Nov 25 '15 at 13:45
  • Here it is: http://pastebin.com/CHcqjGzC – Ron Beyer Nov 25 '15 at 13:46
  • 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? – Rogue Nov 25 '15 at 13:48
  • Here is the resulting file (you can click/drag files into VS to get a hex editor view of the file): http://postimg.org/image/g7c0okfl1/ – Ron Beyer Nov 25 '15 at 13:52
  • @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. – Ron Beyer Nov 25 '15 at 13:54
  • 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. – Rafael Moura Nov 25 '15 at 14:02
  • 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.. – Rafael Moura Nov 25 '15 at 14:20
  • 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.Write7BitEncodedInt`](http://msdn.microsoft.com/de-de/library/system.io.binarywriter.write7bitencodedint%28v=vs.110%29.aspx) – Dirk Vollmar Nov 25 '15 at 14:34
  • 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. – Dirk Vollmar Nov 25 '15 at 14:42
  • @DirkVollmar I've checked the using directives, it's all good, using `System.IO`. Here's a test program: http://pastebin.com/0Cfv3Snc – Rafael Moura Nov 25 '15 at 16:11
  • I've tested on 2 systems here, the problem persists. My files: http://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? – Rafael Moura Nov 25 '15 at 16:41
  • 2
    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. – Hans Passant Nov 25 '15 at 16:50
  • 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! – Rafael Moura Nov 25 '15 at 17:21

1 Answers1

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

Rafael Moura
  • 81
  • 1
  • 6