强制转换的研究

强制转换的研究

/***************************************************
*作 者
*联系方式:
*说 明 :强制转换的研究-(1)
***************************************************/
在编写网络相关程序时,C语言精髓的体现第一精髓就是指针的使用,第二精髓就是强制转换的使用,恰当地利用指针和强制转换不但可以提供程序效率,而且使程序更加之简洁,由于强制转换在C 语言编程中占有重要的地位,下面将已五个比较典型的例子作为讲解。

  • 例子1:将带符号字节整型转换为无符号字节整型
1 UINT8 a=02 INT8 b=-33 a=(UINT8)b;
  • 例子2:在大端模式下(8051 系列单片机是大端模式),将数组a[2]转化为无符号16 位整型
 1 方法1:采用位移方法。
 2     UINT8 a[2]={0x12,0x34};
 3     UINT16 b=0;
 4     b=(a[0]<<8)|a[1];
5     结果:b=0x1234
6 方法2:强制类型转换。 7     UINT8 a[2]={0x12,0x34}; 8     UINT16 b=0; 9     b= *(UINT16 *)a; //强制转换
10     结果:b=0x1234
  • 例子3:保存结构体数据内容。
 1 方法1:逐个保存。
 2 typedef struct _ST
 3 {
 4   UINT8 a;
 5   UINT8 b;
 6   UINT8 c;
 7   UINT8 d;
 8   UINT8 e;
 9 }ST;
10 ST s;
11 UINT8 a[5]={0}; 12   s.a=1; 13   s.b=2; 14   s.c=3; 15   s.d=4; 16   s.e=5; 17   a[0]=s.a; 18   a[1]=s.b; 19   a[2]=s.c; 20   a[3]=s.d; 21   a[4]=s.e; 22 结果:数组a 存储的内容是1、2345
23 方法2:强制类型转换。 24 typedef struct _ST 25 { 26   UINT8 a; 27   UINT8 b; 28   UINT8 c; 29   UINT8 d; 30   UINT8 e; 31 }ST; 32 ST s; 33 UINT8 a[5]={0}; 34 UINT8 *p=(UINT8 *)&s;//强制转换 35 UINT8 i=0; 36   s.a=1; 37   s.b=2; 38   s.c=3; 39   s.d=4; 40   s.e=5; 41 for(i=0;i<sizeof(s);i++) 42 { 43   a[i]=*p++; 44 } 45 结果:数组a 存储的内容是1、2345
  • 例子4:在大端模式下(8051 系列单片机是大端模式)将含有位域的结构体赋给无符号字节整型值
 1 typedef struct __BYTE2BITS
 2 {
 3   UINT8 _bit7:1;
 4   UINT8 _bit6:1;
 5   UINT8 _bit5:1;
 6   UINT8 _bit4:1;
 7   UINT8 _bit3:1;
 8   UINT8 _bit2:1;
 9   UINT8 _bit1:1;
10   UINT8 _bit0:1;
11 }BYTE2BITS;
12 BYTE2BITS Byte2Bits;
13 Byte2Bits._bit7=0;
14 Byte2Bits._bit6=0;
15 Byte2Bits._bit5=1;
16 Byte2Bits._bit4=1;
17 Byte2Bits._bit3=1;
18 Byte2Bits._bit2=1;
19 Byte2Bits._bit1=0;
20 Byte2Bits._bit0=0;
21 UINT8 a=0;
22 a|= Byte2Bits._bit7<<7;
23 a|= Byte2Bits._bit6<<6;
24 a|= Byte2Bits._bit5<<5;
25 a|= Byte2Bits._bit4<<4;
26 a|= Byte2Bits._bit3<<3;
27 a|= Byte2Bits._bit2<<2;
28 a|= Byte2Bits._bit1<<1;
29 a|= Byte2Bits._bit0<<0;
30 结果:a=0x3C

31 方法2:强制转换。 32 typedef struct __BYTE2BITS 33 { 34   UINT8 _bit7:1; 35   UINT8 _bit6:1; 36   UINT8 _bit5:1; 37   UINT8 _bit4:1; 38   UINT8 _bit3:1; 39   UINT8 _bit2:1; 40   UINT8 _bit1:1; 41   UINT8 _bit0:1; 42 }BYTE2BITS; 43 BYTE2BITS Byte2Bits; 44 Byte2Bits._bit7=0; 45 Byte2Bits._bit6=0; 46 Byte2Bits._bit5=1; 47 Byte2Bits._bit4=1; 48 Byte2Bits._bit3=1; 49 Byte2Bits._bit2=1; 50 Byte2Bits._bit1=0; 51 Byte2Bits._bit0=0; 52 UINT8 a=0; 53 a = *(UINT8 *)&Byte2Bits 54 结果:a=0x3C
  • 例子5:在大端模式下(8051 系列单片机是大端模式)将无符号字节整型值赋给含有位域的结构体。
 1 方法1:逐位赋值。
 2 typedef struct __BYTE2BITS
 3 {
 4   UINT8 _bit7:1;
 5   UINT8 _bit6:1;
 6   UINT8 _bit5:1;
 7   UINT8 _bit4:1;
 8   UINT8 _bit3:1;
 9   UINT8 _bit2:1;
10   UINT8 _bit1:1;
11   UINT8 _bit0:1;
12 }BYTE2BITS;
13 BYTE2BITS Byte2Bits;
14 UINT8 a=0x3C;
15 Byte2Bits._bit7=a&0x80;
16 Byte2Bits._bit6=a&0x40;
17 Byte2Bits._bit5=a&0x20;
18 Byte2Bits._bit4=a&0x10;
19 Byte2Bits._bit3=a&0x08;
20 Byte2Bits._bit2=a&0x04;
21 Byte2Bits._bit1=a&0x02;
22 Byte2Bits._bit0=a&0x01;
23 _bit5:1;
24 UINT8 _bit4:1;
25 UINT8 _bit3:1;
26 UINT8 _bit2:1;
27 UINT8 _bit1:1;
28 UINT8 _bit0:1;
29 }BYTE2BITS;
30 BYTE2BITS Byte2Bits;
31 UINT8 a=0x3C;
32 Byte2Bits= *(BYTE2BITS *)&a;

 

 

 

 

posted on 2012-09-18 15:19 竞击 阅读( ...) 评论( ...) 编辑 收藏
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值