您好,登錄后才能下訂單哦!
這篇文章主要講解了“C語言編程中的8位、16位、32位整數的分解與合并方法是什么”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“C語言編程中的8位、16位、32位整數的分解與合并方法是什么”吧!
練習在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";
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編程,想寫個通用的轉換函數,后面想沒有必要,難道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位。
感謝各位的閱讀,以上就是“C語言編程中的8位、16位、32位整數的分解與合并方法是什么”的內容了,經過本文的學習后,相信大家對C語言編程中的8位、16位、32位整數的分解與合并方法是什么這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。