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,
- The prefix number written is in fact an LEB128 encoded count.
- 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
How to Recognize If a String Contains Unicode Chars
How to Copy Value from Class X to Class Y with the Same Property Name in C#
How to Run Commands on Ssh Server in C#
Wpf Binding - Default Value for Empty String
Return Only Digits 0-9 from a String
Raise an Event of a Class from a Different Class in C#
How to Split a Number into Individual Digits in C#
How to Get the Colour of a Pixel at X,Y Using C#
How to Check If Ioexception Is Not-Enough-Disk-Space-Exception Type
Does Dispose Still Get Called When Exception Is Thrown Inside of a Using Statement
How to Create Trial Version of .Net Software
Can You Compile C# So It Doesn't Need the .Net Framework at Runtime
Scaling a System.Drawing.Bitmap to a Given Size While Maintaining Aspect Ratio
How to Change Listview Selected Row Backcolor Even When Focus on Another Control
How to Initialize a Datetime Field