系统大小端的头文件定义

来源:互联网 发布:ubuntu查看显卡使用率 编辑:程序博客网 时间:2024/05/17 23:18

系统大小端的头文件定义

  系统大小端的判断,虽说可以写函数来判断,但有时需要在预处理时就知道大小端,可使用C库头文件<endian.h>来判断,写法入下:

1.#include <endian.h>2.typedef struct {3.    u_short id;     /* query identification number */4.5.#if BYTE_ORDER == BIG_ENDIAN6.            /* fields in third byte */7.    u_char  qr:1;       /* response flag */8.    u_char  opcode:4;   /* purpose of message */9.    u_char  aa:1;       /* authoritive answer */10.    u_char  tc:1;       /* truncated message */11.    u_char  rd:1;       /* recursion desired */12.            /* fields in fourth byte */13.    u_char  ra:1;       /* recursion available */14.    u_char  pr:1;       /* primary server required (non standard) */15.    u_char  unused:2;   /* unused bits */16.    u_char  rcode:4;    /* response code */17.#else18.            /* fields in third byte */19.    u_char  rd:1;       /* recursion desired */20.    u_char  tc:1;       /* truncated message */21.    u_char  aa:1;       /* authoritive answer */22.    u_char  opcode:4;   /* purpose of message */23.    u_char  qr:1;       /* response flag */24.            /* fields in fourth byte */25.    u_char  rcode:4;    /* response code */26.    u_char  unused:2;   /* unused bits */27.    u_char  pr:1;       /* primary server required (non standard) */28.    u_char  ra:1;       /* recursion available */29.#endif30.31.32.            /* remaining bytes */33.    u_short qdcount;    /* number of question entries */34.    u_short ancount;    /* number of answer entries */35.    u_short nscount;    /* number of authority entries */36.    u_short arcount;    /* number of resource entries */37.} HEADER;

这是一个结构体定义,因为使用了位域,所以大小端需要区分对待。


原创粉丝点击