Concept, judgment, and conversion of byte order

Source: Internet
Author: User
Tags htons

1. Concepts of byte order

First, we need to clarify the following two points:

  • A dual-byte data can contain both high-byte and Low-byte data.
  • Two bytes are stored in the memory from low address to high address in sequence.

In this case, the word (double byte) Data 0x1234 is used as an example:

Large-end byte order: the high byte of the word data is stored in the low address of the memory, while the low byte is stored in the high address of the memory. For example, if 0x12 is stored at address a, 0x34 is stored at a + 1 [I .e., high-to-low, low-to-high].

Small-end byte order: the low address where the high byte of the word data is stored in the memory, while the high address where the low byte is stored in the memory. For example, if 0x34 is stored at address a, 0x12 is stored at a + 1 [that is, high-to-high, low-to-low].

Generally, Intel's x-86 family uses the small-end byte order, while network communication uses the big-end byte order.

2. byte order judgment

#define _UNICODE
#define _AFXDLL
#include<tchar.h>
#include<afx.h>
#include<iostream>
using namespace std;
typedef union _ut
{
TCHAR t;
int n;
}ut;
bool IsSmallEndian()
{
ut u;
u.n = 65;
if(u.t=='A')
return true;
else
return false;
}
int _tmain(int argc,TCHAR ** argv)
{
wcout.imbue(std::locale("chs"));
if(IsSmallEndian())
Wcout <_ T ("this computer uses a small terminal") <Endl;
else
Wcout <_ T ("this computer uses big ends") <Endl;
return 1;
}

 

Note that the unique nature of union is used here.

 

3. byte order conversion

 

#if defined(BIG_ENDIAN) && !defined(LITTLE_ENDIAN)
#define htons(A)   (A)
#define htonl(A)     (A)
#define ntohs(A)   (A)
#define ntohl(A)    (A)
#elif defined(LITTLE_ENDIAN) && !defined(BIG_ENDIAN)
#define htons(A)     ((((uint16)(A) & 0xff00) >> 8) | \
(((uint16)(A) & 0x00ff) << 8))
#define htonl(A)     ((((uint32)(A) & 0xff000000) >> 24) | \
(((uint32)(A) & 0x00ff0000) >> 8) | \
(((uint32)(A) & 0x0000ff00) << 8) | \
(((uint32)(A) & 0x000000ff) << 24))
#define ntohs htons
#define ntohl htohl
#else
#error "Either BIG_ENDIAN or LITTLE_ENDIAN must be #defined, but not both."
#endif

 

Note that the shift operation is mainly used.

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.