[c/c++]_[primary]_[judgment size end order Little Endian Order]

Source: Internet
Author: User


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]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.