在單片機的編程中對於8位、16位、32位整數的分解與合併用的比較多,今天作了簡要學習,後面還須要加以總結。編程
練習在VC++6.0編程環境中進行,源程序:
#include <stdio.h>
#include "string.h"ide
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";
unsigned short int p16=(unsigned short int *)(&Data_Uint32);//定義16位的指針將32位地址強制轉爲16位,高位丟棄取低位
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編程,想寫個通用的轉換函數,後面想沒有必要,難道八、16位、32位相互轉換這麼複雜嗎?若是這樣,寫在Keil uVision5裏面的程序會是多麼複雜,之後本身看起來也會繁瑣的。指針
接着寫:code
直接強制轉換:string
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));
實際輸出:it
-------------------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,數據長度:1io
說明:8位強制轉32位,前面系統自動加了24位的0;32位強制轉8位,系統只截取了最後的8位。class