跳至主要内容

【转】考察嵌入式linux开发新人的两道c语言题目

1> 如何判断一个板子的cpu 是big-endian 还是 Little-endian的?

用c实现非常简单,10行左右,就可以判断了,关键考察新人是否了解了什么是endian ,big-endian与little-endian的区别在哪里,如果这些不清楚,就算c再强,也是憋不出来的。

2> 判断了endian 后,如何进行转换,分别用函数和宏来实现。

如果说上面的那个,可能不能正确的考察出新人的c水平,下面这个,可就可以显示了。

尤其是写一个宏, 来实现。 我觉得宏最能体现出一个人的水平了, 大家都知道一个功能强大的,但是写法又非常简单的宏,是不好写的。 尤其是注意类型转换, 大扩号什么的。 写一个函数就容易多了。 

实现起来,或者用宏,或者用函数的形式,都可以,最好都试一下。主要看的就是宏的使用。 

比如:

写成函数的形式:
typedef unsigned int u32 ;
typedef unsigned short u16 ;

u16 bswap16(u16);
u32 bswap32(u32);


写成宏的形式:

#define BSWAP_16(x)
....
#define BSWAP_32(x)
....

比如: 0x1234 变成: 0x3412

或者: 0x12345678 变成 : 0x78563412

--
在下面的回复写出来,就有点乱了,干脆在这里铁出来吧 ,格式比较好:


1》判断endian的问题, 很简单。

判断endian :
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
      short int a = 0x1234;
      char *p = (char *)&a;
    
      printf("p=%#hhx\n",*p);

      if(*p == 0x34)
          printf("Little endian \n");
      else if(*p == 0x12)
          printf("Big endian \n");
      else
          printf("Unknow endian \n");

      return 0;
}


2>如何进行转换:


#include <stdio.h>
#include <stdio.h>

typedef unsigned int u32;
typedef unsigned short u16;

#if 0
//simple: not check varible types

#define BSWAP_16(x) \
            ( (((x) & 0x00ff) << 8 ) | \
       (((x) & 0xff00) >> 8 ) \
       )

//complex:check varible types

#else
#define BSWAP_16(x) \
       (u16) ( ((((u16)(x)) & 0x00ff) << 8 ) | \
                   ((((u16)(x)) & 0xff00) >> 8 ) \
            )


评论

此博客中的热门博文

【转】smb协议栈使用示例

/*  * * uncdownload.c  * *  * * Utility for downloading files from SMB shares using libsmbclient  * *  * * Copyright(C) 2006 Sophos Plc, Oxford, England.  * *  * * This program is free software; you can redistribute it and/or modify it under the terms of the  * * GNU General Public License Version 2 as published by the Free Software Foundation.  * *  * * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without  * * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  * * See the GNU General Public License for more details.  * *  * * You should have received a copy of the GNU General Public License along with this program; if not,  * * write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA  * *  * */ # include < sys / types . h > # include < sys / time . h > # include ...

【转】Ether Types

Ether Types (last updated 2008-09-09) NOTE: Please see [RFC5342] for current information and registration procedures. This registry will be revised soon and will be replaced with up-to-date information. Many of the networks of all classes are Ethernets (10Mb) or Experimental Ethernets (3Mb). These systems use a message "type" field in much the same way the ARPANET uses the "link" field. If you need an Ether Type, contact: IEEE Registration Authority IEEE Standards Department 445 Hoes Lane Piscataway, NJ 08854 Phone +1 732 562 3813 Fax: +1 732 562 1571 Email: <ieee-registration-authority& ieee.org > http://standards.ieee.org/regauth/index.html The following list of EtherTypes is contributed unverified information from various sources. Another list of EtherTypes is maintained by Michael A. Patton and is accessible at: <URL: http://www.cavebear.com/CaveBear/Ethernet/ > <URL: ftp://ftp.cavebear.com/pub/Ethernet-codes > Assign...

【转】tcphdr结构详解

位于:/usr/src/linux/include/linux/tcp.h struct tcphdr { __be16 source; __be16 dest; __be32 seq; __be32 ack_seq; #if defined(__LITTLE_ENDIAN_BITFIELD) __u16   res1:4, doff:4, fin:1, syn:1, rst:1, psh:1, ack:1, urg:1, ece:1, cwr:1; #elif defined(__BIG_ENDIAN_BITFIELD) __u16   doff:4, res1:4, cwr:1, ece:1, urg:1, ack:1, psh:1, rst:1, syn:1, fin:1; #else #error "Adjust your <asm/byteorder.h> defines"