當前位置:首頁 » 編程語言 » 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位元組
大端小端碼的轉換操作。

熱點內容
php辦公系統 發布:2025-07-19 03:06:35 瀏覽:899
奧德賽買什麼配置出去改裝 發布:2025-07-19 02:53:18 瀏覽:40
請與網路管理員聯系請求訪問許可權 發布:2025-07-19 02:37:34 瀏覽:189
ipad上b站緩存視頻怎麼下載 發布:2025-07-19 02:32:17 瀏覽:844
phpcgi與phpfpm 發布:2025-07-19 02:05:19 瀏覽:527
捷達方向機安全登錄密碼是多少 發布:2025-07-19 00:57:37 瀏覽:692
夜魔迅雷下載ftp 發布:2025-07-19 00:39:29 瀏覽:99
增值稅票安全接入伺服器地址 發布:2025-07-19 00:20:45 瀏覽:486
solidworkspcb伺服器地址 發布:2025-07-18 22:50:35 瀏覽:822
怎麼在堆疊交換機里配置vlan 發布:2025-07-18 22:42:35 瀏覽:630