Scene:
1. When reading hard disk data and media data, it is necessary to convert the size end sequence according to the data read to correctly identify the data. One of the processes is to first determine the size of the CPU supported by the order of the case to be converted.
2. Little Endian Order: Data with low address (upper and lower bits of integer).
Test.cpp
#include <stdio.h> #include <iostream>using namespace std;//1. Method 1, by judging whether the low value of the int is stored. int IsLittleEndian1 ( ) { int x = 1; if (* (char*) &x = = 1) { return 1; } else { return 0; }} 1. Method 2, use the properties of the Union to determine the low value int IsLittleEndian2 () {Union w{int A;char B;} w1;w1.a = 1;return (w1.b = = 1);} int main (int argc, char const *argv[]) {cout << "IsLittleEndian1: .....") << endl;if (.) IsLittleEndian1 ()) {cout << "Little Endian Order" << Endl;} Else{cout << "Big Endian Order" << Endl;} cout << "IsLittleEndian2: ..." << endl;if (IsLittleEndian2 ()) {cout << "Little Endian".... Order "<< Endl;} Else{cout << "Big Endian Order" << Endl;} return 0;}
Output:
IsLittleEndian1: ............ Little Endian OrderIsLittleEndian2: ........... Little Endian Order
[c/c++]_[primary]_[judgment size end order Little Endian Order]