做单片机开发或者芯片开发,不可避免地要用到C或者C++,这篇文章是2019年做开发时写的文章,发在CSDN上,展现量是23980,阅读量是13111,收藏量是37,说明对有些开发者还是有用的,就发在今日头条上。
先在VC++6.0编程环境中编写测试代码。
源程序:
#include <stdio.h>
#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;
}
输出:
进入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位。