I designed a C++ code for checking machine’s endian.
It works well. But, it cannot print out each bytes’ contents in a 4-byte int.
#include<iostream>
using namespace std;
bool f()
{
int a = 1;
char *p = (char*)&a;
for (int i = 0 ; i < 4 ; ++i)
cout << "p[" << i << "] is " << hex << *p++ << " ";
cout << endl ;
p -= 4;
if (*p == 1) return true ; // it is little endian
else return false; // it is big endian
}
int main()
{
cout << "it is little endian ? " << f() << endl ;
return 0 ;
}
output:
p[0] is p[1] is p[2] is p[3] is
it is little endian ? 1
Why the output is empty ?
thanks
the issue is that the type of
*pischar, so the stream attempts to print the value of it as an ASCII character (which is likely not the value of a visible character). If you cast it to anintyou will get what you expect: