By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,333 Members | 1,381 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,333 IT Pros & Developers. It's quick & easy.

detect endianness

P: n/a
Hi, I am wondering if C# always assumes small-endian byte order, if not,
how should I test the endianess of the platform, there doesn't seem to be a
way to check as pointers are implicit (as opposed to being explicit in C)?

Thanks
jz
Feb 2 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Jimmy Zhang wrote:
Hi, I am wondering if C# always assumes small-endian byte order, if not,
how should I test the endianess of the platform, there doesn't seem to be a
way to check as pointers are implicit (as opposed to being explicit in C)?

Thanks
jz
Just read the first byte of a short or int with the value 1. If the byte
is 1, the memory model is little-endian.

My recollection of pointer syntax is a bit muddy, but something like
this would do it:

byte b;
unsafe {
int i = 1;
byte* bp = (byte*)&i;
b = *bp;
}

--
Gran Andersson
_____
http://www.guffa.com
Feb 3 '07 #2

P: n/a
Jimmy Zhang wrote:
Hi, I am wondering if C# always assumes small-endian byte order, if not,
how should I test the endianess of the platform, there doesn't seem to be a
way to check as pointers are implicit (as opposed to being explicit in C)?
MS .NET only runs on little endian platforms

Mono runs on both little and big endian platforms.

For obvious performance reasons it uses the native
endianess internally, but that is completely transparent.

It is only relevant when you are exchanging data.

System.IO BinaryReader and BinaryWriter always
uses little endian.

System BitConverter uses actual system endianess.

BitConverter also has a static field IsLittleEndian
you can use to test for.

Arne
Feb 3 '07 #3

P: n/a
Arne Vajhj <ar**@vajhoej.dkwrote:
Jimmy Zhang wrote:
Hi, I am wondering if C# always assumes small-endian byte order, if not,
how should I test the endianess of the platform, there doesn't seem to be a
way to check as pointers are implicit (as opposed to being explicit in C)?
MS .NET only runs on little endian platforms

Mono runs on both little and big endian platforms.

For obvious performance reasons it uses the native
endianess internally, but that is completely transparent.

It is only relevant when you are exchanging data.

System.IO BinaryReader and BinaryWriter always
uses little endian.

System BitConverter uses actual system endianess.

BitConverter also has a static field IsLittleEndian
you can use to test for.
And in addition, if you want complete control, you can use my
EndianBitConvert and EndianBinaryReader/Writer classes:

http://www.pobox.com/~skeet/csharp/miscutil

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 3 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.