Binarywriter Endian Issue

BinaryWriter Endian issue

You can use my EndianBinaryWriter in MiscUtil. That lets you specify the endianness you want. There's also EndianBinaryReader and EndianBitConverter.

EndianBinaryWriter writer = new EndianBinaryWriter(EndianBitConverter.Big,
stream);
writer.Write(...);

It doesn't derive from BinaryWriter, for reasons given in a blog post.

C# BinaryWriter - and endianness

Although 1024 is 0x0400. When it comes to storing this in file or memory,
question comes should we use little endian or big endian notation?

In case of BinaryWriter, it is little endian. Which means LSB goes first - then comes the MSB.
Hence, it is stored as:

LSB | MSB
00 04

You can read more about endianness.

C# binary writer ushort bytes order

According to the MSDN document, BinaryWriter stores UInt16 in little endian format. So it is possible to have a reversed written byte order.

https://msdn.microsoft.com/en-us/library/8sh9zw1e(v=vs.110).aspx

I think you can refer to this post for how to use big endian for the writer.

BinaryWriter Endian issue

C# - Binary reader in Big Endian?

I'm not usually one to answer my own questions, but I've accomplished exactly what I wanted with some simple code:

class BinaryReader2 : BinaryReader { 
public BinaryReader2(System.IO.Stream stream) : base(stream) { }

public override int ReadInt32()
{
var data = base.ReadBytes(4);
Array.Reverse(data);
return BitConverter.ToInt32(data, 0);
}

public Int16 ReadInt16()
{
var data = base.ReadBytes(2);
Array.Reverse(data);
return BitConverter.ToInt16(data, 0);
}

public Int64 ReadInt64()
{
var data = base.ReadBytes(8);
Array.Reverse(data);
return BitConverter.ToInt64(data, 0);
}

public UInt32 ReadUInt32()
{
var data = base.ReadBytes(4);
Array.Reverse(data);
return BitConverter.ToUInt32(data, 0);
}

}

I knew that's what I wanted, but I didn't know how to write it. I found this page and it helped: http://www.codekeep.net/snippets/870c4ab3-419b-4dd2-a950-6d45beaf1295.aspx

Bit shifting c# what's going on here?

The code writes a 16-bit integer in big-endian order. Upper byte is written first. Not the same thing that BinaryWriter does, it writes in little-endian order.

Why Does The Size Of Binary File Does Not Decrease While Using BinaryWriter

This has nothing to do with BinaryWriter, really - it's the File.OpenWrite call, whose documentation includes:

The OpenWrite method opens a file if one already exists for the file path, or creates a new file if one does not exist. For an existing file, it does not append the new text to the existing text. Instead, it overwrites the existing characters with the new characters. If you overwrite a longer string (such as "This is a test of the OpenWrite method") with a shorter string (such as "Second run"), the file will contain a mix of the strings ("Second runtest of the OpenWrite method").

So your second method does only write four bytes - but it overwrites the first four bytes of the file without truncating the file itself.

Use File.Create instead, and any existing file will be truncated.

I'd also advise you to use using statements instead of closing resource manually:

using (var writer = new BinaryWriter(File.Create("foo"))
{
// Code here, no need to call writer.Close()
}

C# BinaryWriter length prefix - UTF7 encoding

Despite what the Microsoft documentation says,

  1. The prefix number written is in fact an LEB128 encoded count.
  2. This is a byte count, not a character count.

The Wiki page I linked gives you decoding code, but I would consider using my own scheme. You could convert the string to UTF8 manually using Encoding.GetBytes() and write that to the MMF, prefixing it with a normal unsigned short. That way you have complete control over everything.



Related Topics



Leave a reply



Submit