open watcom学习记录之——字节序交换

cxdzxc 2012-05-26 11:48:00
int swap32_1(int i)
{
return ((i&0xff000000)>>24)|((i&0xff0000)>>8)|((i&0xff00)<<8)|((i&0xff)<<24);
}

int swap32_2(int i)
{
int sz0[1],*m,n=0;
char sz1[4],*b=(const char *)sz0;
sz0[0]=i;
for (;n<4;)
{
sz1[4-1-n]=b[n];
n++;
}
(const char *)m=sz1;
return m[0];
}

main()
{
int a=0x12345678;
printf("a=%x\n",a);
a=swap32_1(a);
printf("swap32_1--->%x\n",a);
a=0x12345678;
a=swap32_2(a);
printf("swap32_2--->%x",a);
}
...全文
154 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
cxdzxc 2012-06-09
  • 打赏
  • 举报
回复
int swap_3(int x)
{
int out,i;
char *p_in=(const char*)&x,*p_out=(const char*)&out;
for (i=0;i<sizeof(int);p_out[i++]=p_in[sizeof(int)-1-i])
{;}
return out;
}

int bswap(int x)
{
int ret;
/*psrc指向x首字节,pdst指向ret之后*/
char *psrc=(char*)&x,*pdst=(char*)(&ret+1);
while(pdst>(char*)&ret)
*--pdst=*psrc++;
return ret;
}
cxdzxc 2012-06-09
  • 打赏
  • 举报
回复
int swap_3(int x)
{
int out,i;
char *p_in=(const char*)&x,*p_out=(const char*)&out;
for (i=0;i<sizeof(int);p_out[i++]=p_in[sizeof(int)-1-i])
{;}
return out;
}

int bswap(int x)
{
int ret;
/*psrc指向x首字节,pdst指向ret之后*/
char *psrc=(char*)&x,*pdst=(char*)(&ret+1);
while(pdst>(char*)&ret)
*--pdst=*psrc++;
return ret;
}
cxdzxc 2012-06-09
  • 打赏
  • 举报
回复
int swap32_3(int x)
{
int in[1],out[1],i=0;
char *p_in=(const char *)in,*p_out=(const char *)out;
in[0]=x;
for (i;i<4;)
{
p_out[i]=p_in[4-1-i];
i++;
}
return out[0];
}
Lactoferrin 2012-06-06
  • 打赏
  • 举报
回复
噢厉害。
qq120848369 2012-05-28
  • 打赏
  • 举报
回复
噢厉害。
cxdzxc 2012-05-27
  • 打赏
  • 举报
回复
#include <stdio.h>
main()
{
printf("printf(\"%%d\\n\",BUFSIZ)=%d\n",BUFSIZ);
}

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧