當前位置:首頁 » 編程語言 » 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-07-11 07:43:33 瀏覽:387
邁騰有什麼安全配置 發布:2025-07-11 07:42:40 瀏覽:644
c語言字元逆序 發布:2025-07-11 07:41:57 瀏覽:923
怎麼配置交換機的console密碼 發布:2025-07-11 07:41:57 瀏覽:4
東芝存儲卡視頻 發布:2025-07-11 07:41:55 瀏覽:541
cs16為什麼搜不到區域網伺服器 發布:2025-07-11 07:41:21 瀏覽:913
php項目如何上傳伺服器 發布:2025-07-11 07:35:35 瀏覽:182
壓縮機4井 發布:2025-07-11 07:34:57 瀏覽:726
win7訪問共享 發布:2025-07-11 07:34:48 瀏覽:279
telnetlinux退出 發布:2025-07-11 07:29:46 瀏覽:791