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.
BinaryWriter
has an overload to write an array to the file, have you tried just usingbinaryWriter.Write(byteArray)
and skipping the loop? – YcleptBinaryWriter
class fromSystem.IO
which is defined in mscorlib? It might be that you are using anotherBinaryWriter
or that you are calling an extension method which is using another encoding, e.g. a 7-bit encoding such as it is used byBinaryWriter.Write7BitEncodedInt
– Queriusing
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. – QueriSystem.IO
. Here's a test program: pastebin.com/0Cfv3Snc – Stevenson