dpdk 大小端

 

个例子: 

 如果我们将0x1234abcd写入到以0x0000开始的内存中,则结果为 
                  big-endian      little-endian 
 0x0000      0x12                0xcd 
 0x0001      0x34               0xab 
 0x0002      0xab               0x34 
 0x0003      0xcd               0x12 

 除了moto的68K系列和dec的sparc是big endian外,常见的cpu都是little endian。ARM同时支持 big和little,实际应用中通常使用little endian,,Intel系列的CPU就是little endian的。

 

方法一:直接使用看变量的内存值,这里需要使用一些调试技巧。

#include<stdio.h>

void main()
{
    short s=0x1234;
    char * pTest=(char*)&s;
    printf("%p %0X %0X",&s,pTest[0],pTest[1]);
}

以十六进制输出short型变量s在内存中的字节分布。

运行结果为:

                      0012FF7C

                      34 12

https://www.ruanyifeng.com/blog/2016/11/byte-order.html

 

网络字节序一般是指大端传输。

/*
 * Work-around of a compilation error with ICC on invocations of the
 * rte_be_to_cpu_16() function.
 */
#ifdef __GCC__
#define RTE_BE_TO_CPU_16(be_16_v)  rte_be_to_cpu_16((be_16_v))
#define RTE_CPU_TO_BE_16(cpu_16_v) rte_cpu_to_be_16((cpu_16_v))
#else
#if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
#define RTE_BE_TO_CPU_16(be_16_v)  (be_16_v)
#define RTE_CPU_TO_BE_16(cpu_16_v) (cpu_16_v)
#else
#define RTE_BE_TO_CPU_16(be_16_v) \
        (uint16_t) ((((be_16_v) & 0xFF) << 8) | ((be_16_v) >> 8))
#define RTE_CPU_TO_BE_16(cpu_16_v) \
        (uint16_t) ((((cpu_16_v) & 0xFF) << 8) | ((cpu_16_v) >> 8))
#endif
#endif /* __GCC__ */

 

 

 

posted on 2020-09-16 10:49  tycoon3  阅读(818)  评论(0)    收藏  举报

导航