473,385 Members | 1,757 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,385 software developers and data experts.

Endianness

hi folks
I am new to this group.i need to know how i can find the endianness of
my system.help me out.

Nov 15 '05 #1
7 4202


bush wrote:
hi folks
I am new to this group.i need to know how i can find the endianness of
my system.help me out.


The best way is not to ask the question in the first place.
Write code that doesn't care about endianness -- more generally,
that cares only about the values and not about how they are
represented.

Sometimes the best way isn't practical (although IMHO it
*is* practical far more often than people seem to think). In
that case, the second-best way to find out is to read your
system's documentation.

Sometimes even the second-best way won't do; you might
need a method suitable for use by the running program. The
third-best way is to store selected values into an `int' (or
whatever) and then inspect the individual bytes for clues.
Be warned: There are more than two possibilities! There are,
for example, twenty-four ways to arrange the bytes of a four-
byte `int', and at least three of them have been used in real
machines.

The worst way of all is to imagine that you know the answer
without needing to ask.

--
Er*********@sun.com

Nov 15 '05 #2
bush wrote:
hi folks
I am new to this group.i need to know how i can find the endianness of
my system.help me out.

#include <stdio.h>

int main()
{
int n = 0x04030201;
char *s = (void *)&n;

printf("%d%d%d%d\n", s[0], s[1], s[2], s[3]);

return 0;
}

This will print '4321' on a big-endian system, like a mac G5 or a SUN
SPARC. On a pc it will print '1234', meaning it's little-endian. You
can of course simplify this into only two bytes if you like, it's just
what I had floating around on my disk. Depends on what you're using it
for, because then you can't see if it's a 'middle-endian' cpu (on a
PDP-11 it should print '3412', but those are rare anyway.

Hope this was what you meant. The clue is to cast the address of an int
or short to (void *) or (char *), because then it won't get converted
into the big-endian order when you print it.
Nov 15 '05 #3
bush wrote:
hi folks
I am new to this group.i need to know how i can find the endianness of
my system.help me out.

#include <stdio.h>

int main()
{
int n = 0x04030201;
char *s = (char *)&n;

printf("%d%d%d%d\n", s[0], s[1], s[2], s[3]);

return 0;
}

This will print '4321' on a big-endian system, like a mac G5 or a SUN
SPARC. On a pc it will print '1234', meaning it's little-endian. You
can of course simplify this into only two bytes if you like, it's just
what I had floating around on my disk. Depends on what you're using it
for, because then you can't see if it's a 'middle-endian' cpu (on a
PDP-11 it should print '3412', but those are rare anyway.

Hope this was what you meant. The clue is to cast the address of an int
or short to (void *) or (char *), because then it won't get converted
into the big-endian order when you print it.
Nov 15 '05 #4
On Wed, 22 Jun 2005 14:01:03 -0400, Eric Sosman wrote:


bush wrote:
hi folks
I am new to this group.i need to know how i can find the endianness of
my system.help me out.


The best way is not to ask the question in the first place.
Write code that doesn't care about endianness -- more generally,
that cares only about the values and not about how they are
represented.


That's fine, if your code is C, and C alone. However, for performance
reasons, it is sometimes convenient to invoke assembly language functions
- and then, taking endianness into account matters.
Nov 15 '05 #5
Felix Rawlings wrote:
On Wed, 22 Jun 2005 14:01:03 -0400, Eric Sosman wrote:


bush wrote:
hi folks
I am new to this group.i need to know how i can find the endianness of
my system.help me out.


The best way is not to ask the question in the first place.
Write code that doesn't care about endianness -- more generally,
that cares only about the values and not about how they are
represented.

That's fine, if your code is C, and C alone. However, for performance
reasons, it is sometimes convenient to invoke assembly language functions
- and then, taking endianness into account matters.


1) I did mention that the strait and narrow way is not
always practical.

2) While there are sometimes reasons to stray from the
strait and narrow, in my experience the need to chit-chat
with other languages *on the same machine* has not been
among them. YMMV, of course, but discussions of the unpleasant
particulars probably belong on system-specific newsgroups.

--
Eric Sosman
es*****@acm-dot-org.invalid
Nov 15 '05 #6
On Thu, 23 Jun 2005 01:14:09 +0000, Felix Rawlings wrote:
On Wed, 22 Jun 2005 14:01:03 -0400, Eric Sosman wrote:


bush wrote:
hi folks
I am new to this group.i need to know how i can find the endianness of
my system.help me out.


The best way is not to ask the question in the first place.
Write code that doesn't care about endianness -- more generally,
that cares only about the values and not about how they are
represented.


That's fine, if your code is C, and C alone. However, for performance
reasons, it is sometimes convenient to invoke assembly language functions
- and then, taking endianness into account matters.


Unlikely, at least as far as the C code is concerned. The assembly
programmer will know the byte order of the architecture and it is rather
unlikely that the C implementation will use a different byte order to
this. So if, for example, you pass an int (somehow) to an assembly routine
you'll end up with a "word", value in a register or whatever in the
correct byte order for the assembly code to use. There is no need for the
code to incorporate any explicit knowledge of byte order. When it is
needed explicitly it is typically the responsibility of the assembly
code to do the right thing rather than the C code.

Consider for example that an implementation could implement standard
library functions in assembly, so your C code might be calling code
written in assembly without you even being aware of it. It just works as
far as the C code is concerned.

Lawrence
Nov 15 '05 #7
On Wed, 22 Jun 2005 19:14:40 GMT, Tydr Schnubbis <fa**@address.dude>
wrote:
<snip>
int n = 0x04030201;
char *s = (char *)&n;

printf("%d%d%d%d\n", s[0], s[1], s[2], s[3]);

return 0;
}

This will print '4321' on a big-endian system, like a mac G5 or a SUN
SPARC. On a pc it will print '1234', meaning it's little-endian. You
(Only) if char is 8 bits, which is true of nearly all platforms
including those you named but not all and not required by the C
standard; and there are no embedded or leading padding bits in int,
again very common but not required.
can of course simplify this into only two bytes if you like, it's just
what I had floating around on my disk. Depends on what you're using it
for, because then you can't see if it's a 'middle-endian' cpu (on a
PDP-11 it should print '3412', but those are rare anyway.

Only if you make it 'long'; PDP-11 'int' was 16 bits (2 bytes of 8).
- David.Thompson1 at worldnet.att.net
Nov 15 '05 #8

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
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...

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.