473,396 Members | 2,036 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

detect endianness

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
3 4517
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;
}

--
Göran Andersson
_____
http://www.guffa.com
Feb 3 '07 #2
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
Arne Vajhøj <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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: kelvSYC | last post by:
Are there any endianness concerns in C++, or does the compiler take care of those details? I ask because I'm not sure if code such as the following have consistent behavior on all platforms. ...
26
by: Case | last post by:
#include <string.h> int i; /* 4-byte == 4-char */ char data = { 0x78, 0x56, 0x34, 0x12 }; int main() { memcpy(&i, data, 4); /*
15
by: T Koster | last post by:
Hi group, I'm having some difficulty figuring out the most portable way to read 24 bits from a file. This is related to a Base-64 encoding. The file is opened in binary mode, and I'm using...
2
by: SSM | last post by:
Hi, Does C standard comment about "Endianness" to be used to store a structure/union variables? Thanks & Regards, Mehta
72
by: gamehack | last post by:
Hi all, I was thinking today, suppose we have the number n = 0xAB 0xFF which is equivalent to 44031 in decimal. In big endian it will be stored as 10101011 11111111 but in little endian...
18
by: friend.05 | last post by:
Code to check endianness of machine
6
by: Tomás | last post by:
Let's say you want to write fully portable code that will be writing files or sending data, and the data is text encoded using Unicode 16-Bit. Endianness comes into play. I'm writing code at the...
134
by: jacob navia | last post by:
Hi Suppose you have somewhere #define BOOL int and somewhere else typedef BOOL int;
18
by: Indian.croesus | last post by:
Hi, If I am right Endianness is CPU related. I do not know if the question is right in itself but if it is then how does C handle issues arising out of Endianness. I understand that if we pass...
5
by: Rahul | last post by:
Hi Everyone, I have a program unit which does >and << of an integer which is of 4 bytes length. The logic of shifting and action based on the result, assumes that the system is big-endian. ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.