Chinaunix首页 | 论坛 | 博客
  • 博客访问: 313948
  • 博文数量: 101
  • 博客积分: 2428
  • 博客等级: 大尉
  • 技术积分: 1050
  • 用 户 组: 普通用户
  • 注册时间: 2010-10-24 13:16
文章分类

全部博文(101)

文章存档

2012年(3)

2011年(90)

2010年(8)

分类: LINUX

2011-04-29 16:39:48

文章说明:calmarrow(lqm)原创

文章引自:http://piaoxiang.cublog.cn

 
    一个16位整数,由两个字节组成。内存中存储这两个字节有两种方法:一种是将低序字节存储在起始地址,称为little-endian字节序,另一种方法是将高序字节存储在起始地址,称为big-endian字节序。术语little-endian和big-endian表示多字节的哪一端存储在该值的起始地址。
 
    例如:0x0102
    little-endian: A    0x02
                   A+1  0x01
    big-endian:    A    0x01
                   A+1  0x02
 
    UNP上面有测试字节排序的函数。考虑到网络字节序为big-endian,修改程序来比较一下Host Byte Order和Internet Byte Order的区别。
 

/*
 * filename: byteorder.c
 */


#include <stdio.h>
#include <netinet/in.h>

union {
        short s;
        char c[sizeof(short)];
}un;
short test = 0x0203;

void byteorder(char *msg)
{
        printf("%s", msg);
        if (sizeof(short) == 2) {
                if (un.c[1] == (test & 0x00ff)) {
                        printf("big-endian\n");
                }
                else if (un.c[0] == (test & 0x00ff)) {
                        printf("small-endian\n");
                }
                else {
                        printf("unkowned\n");
                }
        }
        else {
                printf("sizeof(short) = %d\n", sizeof(short));
        }
}

int main(void)
{
        char *msg1 = "Host Byte Order: ";
        char *msg2 = "Network Byte Order: ";

        un.s = test;
        byteorder(msg1);
        un.s = htons(test);
        byteorder(msg2);

        return 0;
}

测试结果如下:

[armlinux@lqm byteorder]$ ./byteorder
Host Byte Order: small-endian
Network Byte Order: big-endian

    在进行嵌入式移植的时候,要考虑到字节序的问题。网络编程更是要考虑字节序的转换,一般使用四个函数htons、htonl、ntohs、ntohl。其中,h代表host,n代表network,s代表short,l代表long。在这里,把s看成一个16位的值(如TCP或UDP的端口号),把l看作32位的值(如IPv4)。要进行IPv6的编程的话,需要进行相应的改变了。

2008/01/17

    想到了两个问题。一个是如何可靠的判断存储格式是大端还是小端,另一个是如何进行大小端转换。自己写了程序,可以实现,但是发现在细节上考虑的还是不周全。参考了一下Uboot的源代码,实现如下:

#ifndef ENDIAN_H_
#define ENDIAN_H_

#include <types.h>

#define BIG_ENDIAN 0
#define SMALL_ENDIAN 1

typedef union {
        u16 a;
        u8 b[2];
}ENDIAN;

/* judge cpu is big endian(0) or small endian(1) */
bool judge_endian(void)
{
        ENDIAN t;

        t.a = 0x0001U;

        return (bool)(t.b[0] == 1);
}

/* type translation */
#define SWAB16(x) \
                ((u16)( \
                        ((((u16)(x) & (u16)0x00FFU)) << 8) | \
                        ((((u16)(x) & (u16)0xFF00U)) >> 8)))

#define SWAP32(x) \
                ((u32)( \
                        ((((u32)(x) & (u32)0x000000FFUL)) << 24) | \
                        ((((u32)(x) & (u32)0x0000FF00UL)) << 8) | \
                        ((((u32)(x) & (u32)0x00FF0000UL)) >> 8) | \
                        ((((u32)(x) & (u32)0xFF000000UL)) >> 24)))

#endif /* ENDIAN_H_ */

阅读(713) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~