當前位置:首頁 » 編程語言 » c語言位元組交換

c語言位元組交換

發布時間: 2023-06-15 19:56:38

c語言 對位元組的高位和低位進行互換!

可以直接用位運算:按位與,按位或,移位等

#include "stdio.h"
int main()
{
unsigned char tmp1,tmp2;

printf("please input a char: ");
scanf("%c", &tmp1);
tmp2=
((tmp1&0x01)<<7)
|((tmp1&0x02)<<5)
|((tmp1&0x04)<<3)
|((tmp1&0x08)<<1)
|((tmp1&0x10)>>1)
|((tmp1&0x20)>>3)
|((tmp1&0x40)>>5)
|((tmp1&0x80)>>7);
printf("converted char is: %c\n", tmp2);
return 0;
}

Ⅱ c語言實現2個位元組的高低位轉化

2個位元組short
int
高低位轉化:
short
int
y=0x7f21;
y
=
(
(y
&
0xff00)
>>
8)
|
(
(y
&
0x00ff
)
<<
8);
printf("%#x",y);
-------------------------------
也可以利用
union
union
B2
{
short
int
i;
unsigned
char
c[2];
//
。。。任意兩位元組類型
}
;
union
B2
x;
unsigned
char
tmp;
x.i=0x1234;
tmp
=
x.c[0];
x.c[0]=x.c[1];x.c[1]=tmp;
//
也可以用上面的位運算方法交換高低端
printf("%#x\n",x.i);
//
union
方法特別適合4位元組8位元組
大端小端碼的轉換操作。

熱點內容
銳志哪個配置性價比最高 發布:2025-02-12 17:38:43 瀏覽:918
智能推送演算法 發布:2025-02-12 17:38:41 瀏覽:835
拍照上傳器 發布:2025-02-12 17:34:29 瀏覽:652
androidweb框架 發布:2025-02-12 17:32:45 瀏覽:76
安卓編程賀卡 發布:2025-02-12 17:32:44 瀏覽:838
php獲取資料庫的欄位 發布:2025-02-12 17:29:02 瀏覽:766
伺服器地址消失 發布:2025-02-12 17:23:36 瀏覽:951
後台執行php腳本 發布:2025-02-12 17:21:45 瀏覽:471
spring編程式事務 發布:2025-02-12 17:16:55 瀏覽:398
nginx禁止ip訪問 發布:2025-02-12 17:15:14 瀏覽:273