C语言编程中的8位、16位、32位整数的分解与合并

  在单片机的编程中对于8位、16位、32位整数的分解与合并用的比较多,今天做了简要学习,后面还需要加以总结。

  练习在VC++6.0编程环境中进行,源程序:

#include 
#include "string.h"

int main(int argc, char* argv[])
{    
	  unsigned int Data_Uint32=0x12345678;
	  unsigned short int Data_Uint16_1,Data_Uint16_2;
	  unsigned char Data_Uint8_1,Data_Uint8_2,Data_Uint8_3,Data_Uint8_4;
	  struct StructByte8{
		  unsigned char Byte01:1;
		  unsigned char Byte02:1;
		  unsigned char Byte03:1;
		  unsigned char Byte04:1;
		  unsigned char Byte05:1;
		  unsigned char Byte06:1;
		  unsigned char Byte07:1;
		  unsigned char Byte08:1;
	  } Test1;
      unsigned char C1[]="A";
      //定义16位的指针将32位地址强制转为16位,高位丢弃取低位
	  unsigned short int *p16=(unsigned short int *)(&Data_Uint32);
	  unsigned short int Data_Uint16_2p,Data_Uint16_1p;

	  printf("32位整数:0x%x\n",Data_Uint32);
	  printf("-------------------通过指针运算-------------------\n");
	  Data_Uint16_2p=*(unsigned short int *)p16;
	  Data_Uint16_1p=*((unsigned short int *)p16+1);
	  printf("转换后的16位整数:0x%x,0x%x\n",Data_Uint16_1p,Data_Uint16_2p);

	  Data_Uint8_1= *(unsigned char *)p16;
	  Data_Uint8_2= *((unsigned char *)p16+1);
	  Data_Uint8_3= *((unsigned char *)p16+2);
	  Data_Uint8_4= *((unsigned char *)p16+3);
	  printf("转换后的8位整数:0x%x,0x%x,0x%x,0x%x\n",Data_Uint8_1,Data_Uint8_2,
                                                     Data_Uint8_3,Data_Uint8_4);
	  Data_Uint8_1=0;Data_Uint8_2=0;Data_Uint8_3=0;Data_Uint8_4=0;
	  //直接根据指针取值
	  //将32位的整数分解成两个16位的整数,再取低位的16位
	  //强制转换,丢弃高位的16位
	  //32位转16位
	  Data_Uint16_1=(unsigned short int)(Data_Uint32>>16);
	  Data_Uint16_2=(unsigned short int)Data_Uint32;

	  //32位转8位
	  Data_Uint8_1= (unsigned char)(Data_Uint32>>24);
	  Data_Uint8_2= (unsigned char)(Data_Uint32>>16);
	  Data_Uint8_3= (unsigned char)(Data_Uint32>>8);
	  Data_Uint8_4= (unsigned char)Data_Uint32;

	  printf("-------------------通过位运算-------------------\n");
	  printf("转换后的16位整数:0x%x,0x%x\n",Data_Uint16_1,Data_Uint16_2);
	  printf("转换后的8位整数:0x%x,0x%x,0x%x,0x%x\n",Data_Uint8_1,Data_Uint8_2,
                                                    Data_Uint8_3,Data_Uint8_4);
	  
	  printf("-------------------通过结构运算-------------------\n");     
	  printf("字符A,ASCII为01000001,结构转换后的内容\n");  
      memcpy(&Test1, C1, sizeof(Test1));  
	  printf("Test1.Byte08=%d \n",Test1.Byte08);  
	  printf("Test1.Byte07=%d \n",Test1.Byte07);  
	  printf("Test1.Byte06=%d \n",Test1.Byte06);  
	  printf("Test1.Byte05=%d \n",Test1.Byte05);  
	  printf("Test1.Byte04=%d \n",Test1.Byte04);  
	  printf("Test1.Byte03=%d \n",Test1.Byte03);  
	  printf("Test1.Byte02=%d \n",Test1.Byte02);  
	  printf("Test1.Byte01=%d \n",Test1.Byte01);  
      
	  Data_Uint8_1=0x89;Data_Uint8_2=0xAB;Data_Uint8_3=0xCD;Data_Uint8_4=0xEF;
	  printf("-------------------通过位运算合并-------------------\n");
	  Data_Uint16_1 = (unsigned short int)(Data_Uint8_1 << 8) | (unsigned short int)(Data_Uint8_2);
	  Data_Uint16_2 = (unsigned short int)(Data_Uint8_3 << 8) | (unsigned short int)(Data_Uint8_4);
	  Data_Uint32=(unsigned int) (Data_Uint16_1 << 16) | (unsigned int)(Data_Uint16_2);
	  printf("合并前的8位整数:0x%x,0x%x,0x%x,0x%x\n",Data_Uint8_1,Data_Uint8_2,Data_Uint8_3,Data_Uint8_4);
	  printf("合并后的16位整数:0x%x,0x%x\n",Data_Uint16_1,Data_Uint16_2);
	  printf("合并后的32位整数:0x%x\n",Data_Uint32);

	return 0;
}

输出:

C语言编程中的8位、16位、32位整数的分解与合并_第1张图片

在这里练习以后进入Keil uVision5编程,想写个通用的转换函数,后面想没有必要,难道8、16位、32位相互转换这么复杂吗?如果这样,写在Keil uVision5里面的程序会是多么复杂,以后自己看起来也会繁琐的。

接着写:

直接强制转换:

	  printf("-------------------8位到32位转换-------------------\n");
	  Data_Uint8_1=0x00;
	  Data_Uint8_2=0x19;
	  Data_Uint16_2=0x00;
	  Data_Uint16_1 = (unsigned short int)(0x00 << 8) | (unsigned short int)(Data_Uint8_2);
      Data_Uint32=(unsigned int) (0x00 << 16) | (unsigned int)(Data_Uint16_1);
	  printf("转换前的8位整数:0x%x,%d\n",Data_Uint8_2,sizeof(Data_Uint8_2));
	  printf("8位到32位转换1======>转换后的32位整数:0x%x,数据长度:%d\n",Data_Uint32,sizeof(Data_Uint32));
	  Data_Uint32=(unsigned int) (0x00 << 16) | (unsigned int)( (unsigned short int)(0x00 << 8) | (unsigned short int)(Data_Uint8_2));
	  printf("8位到32位转换2======>转换后的32位整数:0x%x,数据长度:%d\n",Data_Uint32,sizeof(Data_Uint32));
	  //Data_Uint32=(unsigned int) (0x00 << 24) | (unsigned int)(Data_Uint8_2);
	  Data_Uint32= (unsigned int)(Data_Uint8_2);
	  printf("8位到32位转换3======>转换后的32位整数:0x%x,数据长度:%d\n",Data_Uint32,sizeof(Data_Uint32));
      Data_Uint32=0x69;
      Data_Uint8_1= (unsigned char)(Data_Uint32);
	  printf("32位整数:0x%x,数据长度:%d\n",Data_Uint32,sizeof(Data_Uint32));
	  printf("32位到8位转换======>转换后的8位整数:0x%x,数据长度:%d\n",Data_Uint8_1,sizeof(Data_Uint8_1));

实际输出:

-------------------8位到32位转换-------------------
转换前的8位整数:0x19,1
8位到32位转换1======>转换后的32位整数:0x19,数据长度:4
8位到32位转换2======>转换后的32位整数:0x19,数据长度:4
8位到32位转换3======>转换后的32位整数:0x19,数据长度:4
32位整数:0x69,数据长度:4
32位到8位转换======>转换后的8位整数:0x69,数据长度:1

 

说明:8位强制转32位,前面系统自动加了24位的0;32位强制转8位,系统只截取了最后的8位。

你可能感兴趣的:(VC++/C++/C)