這篇文章主要講解了“C語言編程中的8位、16位、32位整數(shù)的分解與合并方法是什么”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“C語言編程中的8位、16位、32位整數(shù)的分解與合并方法是什么”吧!
讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來自于我們對這個行業(yè)的熱愛。我們立志把好的技術(shù)通過有效、簡單的方式提供給客戶,將通過不懈努力成為客戶在信息化領(lǐng)域值得信任、有價值的長期合作伙伴,公司提供的服務(wù)項目有:空間域名、虛擬空間、營銷軟件、網(wǎng)站建設(shè)、北票網(wǎng)站維護、網(wǎng)站推廣。
練習(xí)在VC++6.0編程環(huán)境中進行,源程序:
#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位地址強制轉(zhuǎn)為16位,高位丟棄取低位
unsigned short int Data_Uint16_2p,Data_Uint16_1p;
printf("32位整數(shù):0x%x\n",Data_Uint32); printf("-------------------通過指針運算-------------------\n"); Data_Uint16_2p=*(unsigned short int *)p16; Data_Uint16_1p=*((unsigned short int *)p16+1); printf("轉(zhuǎn)換后的16位整數(shù):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("轉(zhuǎn)換后的8位整數(shù):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; //直接根據(jù)指針取值 //將32位的整數(shù)分解成兩個16位的整數(shù),再取低位的16位 //強制轉(zhuǎn)換,丟棄高位的16位 //32位轉(zhuǎn)16位 Data_Uint16_1=(unsigned short int)(Data_Uint32>>16); Data_Uint16_2=(unsigned short int)Data_Uint32; //32位轉(zhuǎn)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("轉(zhuǎn)換后的16位整數(shù):0x%x,0x%x\n",Data_Uint16_1,Data_Uint16_2); printf("轉(zhuǎn)換后的8位整數(shù):0x%x,0x%x,0x%x,0x%x\n",Data_Uint8_1,Data_Uint8_2,Data_Uint8_3,Data_Uint8_4); printf("-------------------通過結(jié)構(gòu)運算-------------------\n"); printf("字符A,ASCII為01000001,結(jié)構(gòu)轉(zhuǎn)換后的內(nèi)容\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位整數(shù):0x%x,0x%x,0x%x,0x%x\n",Data_Uint8_1,Data_Uint8_2,Data_Uint8_3,Data_Uint8_4); printf("合并后的16位整數(shù):0x%x,0x%x\n",Data_Uint16_1,Data_Uint16_2); printf("合并后的32位整數(shù):0x%x\n",Data_Uint32); return 0;
}
輸出:
在這里練習(xí)以后進入Keil uVision5編程,想寫個通用的轉(zhuǎn)換函數(shù),后面想沒有必要,難道8、16位、32位相互轉(zhuǎn)換這么復(fù)雜嗎?如果這樣,寫在Keil uVision5里面的程序會是多么復(fù)雜,以后自己看起來也會繁瑣的。
接著寫:
直接強制轉(zhuǎn)換:
printf("-------------------8位到32位轉(zhuǎn)換-------------------\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("轉(zhuǎn)換前的8位整數(shù):0x%x,%d\n",Data_Uint8_2,sizeof(Data_Uint8_2)); printf("8位到32位轉(zhuǎn)換1======>轉(zhuǎn)換后的32位整數(shù):0x%x,數(shù)據(jù)長度:%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位轉(zhuǎn)換2======>轉(zhuǎn)換后的32位整數(shù):0x%x,數(shù)據(jù)長度:%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位轉(zhuǎn)換3======>轉(zhuǎn)換后的32位整數(shù):0x%x,數(shù)據(jù)長度:%d\n",Data_Uint32,sizeof(Data_Uint32)); Data_Uint32=0x69; Data_Uint8_1= (unsigned char)(Data_Uint32); printf("32位整數(shù):0x%x,數(shù)據(jù)長度:%d\n",Data_Uint32,sizeof(Data_Uint32)); printf("32位到8位轉(zhuǎn)換======>轉(zhuǎn)換后的8位整數(shù):0x%x,數(shù)據(jù)長度:%d\n",Data_Uint8_1,sizeof(Data_Uint8_1));
實際輸出:
-------------------8位到32位轉(zhuǎn)換-------------------
轉(zhuǎn)換前的8位整數(shù):0x19,1
8位到32位轉(zhuǎn)換1======>轉(zhuǎn)換后的32位整數(shù):0x19,數(shù)據(jù)長度:4
8位到32位轉(zhuǎn)換2======>轉(zhuǎn)換后的32位整數(shù):0x19,數(shù)據(jù)長度:4
8位到32位轉(zhuǎn)換3======>轉(zhuǎn)換后的32位整數(shù):0x19,數(shù)據(jù)長度:4
32位整數(shù):0x69,數(shù)據(jù)長度:4
32位到8位轉(zhuǎn)換======>轉(zhuǎn)換后的8位整數(shù):0x69,數(shù)據(jù)長度:1
說明:8位強制轉(zhuǎn)32位,前面系統(tǒng)自動加了24位的0;32位強制轉(zhuǎn)8位,系統(tǒng)只截取了最后的8位。
感謝各位的閱讀,以上就是“C語言編程中的8位、16位、32位整數(shù)的分解與合并方法是什么”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對C語言編程中的8位、16位、32位整數(shù)的分解與合并方法是什么這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識點的文章,歡迎關(guān)注!
當(dāng)前名稱:C語言編程中的8位、16位、32位整數(shù)的分解與合并方法是什么
文章網(wǎng)址:http://jinyejixie.com/article32/ghddpc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)建站、網(wǎng)站建設(shè)、網(wǎng)站制作、網(wǎng)站收錄、網(wǎng)站設(shè)計公司、網(wǎng)站設(shè)計
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)