Solved

Get platform to know the endians

Posted on 2004-10-19
2
220 Views
Last Modified: 2010-04-01
Dear experts,
I need to know how to write a platform-indep C/C++ code that get the platform of the running machine. I need to know whether it is a big endian or little endian machine.

Thanx for fast response,
Amr
0
Comment
Question by:amradel_79
2 Comments
 
LVL 12

Expert Comment

by:OnegaZhang
ID: 12346282
   int data = 0;
    char *p = (char*)&data;
    p[0] = 1;
    p[1] = 2;
    p[2] = 3;
    p[3] = 4;
    if(data == 0x01020304)
        std::cout<<"big endian"<<std::endl;
    else
        std::cout<<"little endian"<<std::endl;
0
 
LVL 48

Accepted Solution

by:
AlexFM earned 500 total points
ID: 12346301
http://upc.pkmn.co.uk/win32/littleend.html

inline int IsLittleEndian(){
  int i = 1 ;
  return (*((char *) &i )) ? 1 : 0;
}

0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now