C# little endian or big endian?
C# itself doesn't define the endianness. Whenever you convert to bytes, however, you're making a choice. The BitConverter class has an IsLittleEndian field to tell you how it will behave, but it doesn't give the choice. The same goes for BinaryReader/BinaryWriter.
My MiscUtil library has an EndianBitConverter class which allows you to define the endianness; there are similar equivalents for BinaryReader/Writer. No online usage guide I'm afraid, but they're trivial :)
(EndianBitConverter also has a piece of functionality which isn't present in the normal BitConverter, which is to do conversions in-place in a byte array.)
Converting Big endian to Little endian in C#
Take a look at the BitConverter
First you check if your system is little endian or not and then reverse the bytes depending on that.
float num = 1.2f;
if (!BitConverter.IsLittleEndian)
{
byte[] bytes = BitConverter.GetBytes(num);
Array.Reverse(bytes, 0, bytes.Length);
num = BitConverter.ToSingle(bytes, 0);
}
Console.WriteLine(num);
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
Efficient way to read big endian data in C#
BitConverter.ToInt32
isn't very fast in the first place. I'd simply use
public static int ToInt32BigEndian(byte[] buf, int i)
{
return (buf[i]<<24) | (buf[i+1]<<16) | (buf[i+2]<<8) | buf[i+3];
}
You could also consider reading more than 4 bytes at a time.
To big endian or to little endian?
Actually the answer is it depends
If you just want a choice then Since in Big Endian high order byte comes first ,you can always check positive or negative from the first byte.
Are strings ALWAYS Little Endian Unicode?
The CLI specification says:
I II.1.1.3 Character data type
A CLI char type occupies 2 bytes in memory and represents a Unicode code unit using UTF-16
encoding.
There is no requirement that it be in a particular byte-order. And there are good reasons to expect that the byte order would match the byte order for other numeric types for the current architecture. I.e. on a big-endian machine, one would expect the char
type to be stored as big-endian 16-bit values.
While it's not an authoritative document, I'll note that several people who have answered or commented on How do I get a consistent byte representation of strings in C# without manually specifying an encoding? share this belief, i.e. that endianness of the char
type depends on the platform architecture. There are several statements in the comments and answers to that question that claim that char
is big-endian on big-endian systems.
It seems to me that if the endianness of your architecture is important, you would have access to a CLI implementation for a big-endian architecture and would be able to easily verify for yourself the byte order used for the char
type. Have you made any effort to do such a verification?
All that said, it is very likely that you do not need to know the byte ordering for the char
type. .NET provides character encoders for a wide variety of encodings, including both UTF16-LE and UTF16-BE. When using the char
type itself, the byte ordering is irrelevant, and in situations where the byte ordering matters, you can force a specific ordering by using the appropriate Encoding
type. If you feel that you have a situation which you believe is an exception to these general guidelines, it would be better to post a question describing exactly what that situation is and why you believe it's an exception to the general guidelines.
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.
Related Topics
Redirect from Action Filter Attribute
Performance Difference for Control Structures 'For' and 'Foreach' in C#
Reading Excel File Using Oledb Data Provider
How to Check If a String Exists in Another String
Web API Put Request Generates an Http 405 Method Not Allowed Error
Why Should I Use Int Instead of a Byte or Short in C#
Implementing Zero or One to Zero or One Relationship in Ef Code First by Fluent API
Can't Add Script Component Because the Script Class Cannot Be Found
What Does "Where T:Class, New()" Mean
How to Wait for a Backgroundworker to Cancel
How to Do Template Specialization in C#
How to Get Client Date and Time in ASP.NET
Format Timespan Greater Than 24 Hour
Difference Between Invoke and Dynamicinvoke
Managing Multiple Selections with Mvvm