如何对字节进行高低位变换(位移)

kudosky 2008-08-19 11:04:02
如何对字节进行高低位变换(位移)
从文件读取的数据是从高位到低位写的
我想转换过来,具体应该怎么做啊?〉
比如:

int a;
short int b;
...全文
3218 19 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
ysysbaobei 2008-08-22
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 ysysbaobei 的回复:]
不知道LZ的确切含义
1.0xABCDEF89 转换为:0xEF89ABCD 还是:0x98FEDCBA ?
2.0xABCDEF 转换为:0xCDEF00AB 还是:0xCDEFAB 还是:0xFEDCBA 还是:0xFEDCBA00 ?
[/Quote]

麻烦LZ把问题详细化,举几个实例,好几个回复者,都是在猜测你题目的含义
kudosky 2008-08-22
  • 打赏
  • 举报
回复
谢谢各位的关心,我现在就去试试!
gaomingok 2008-08-22
  • 打赏
  • 举报
回复

long li=0x01020304;
long res=0; //反转后的结果
for(int i=0;i<4;i++)
{
res=res*256+((li>>(i*8))&0xff);
}

根据字节的多少改变for循环的次数
ysysbaobei 2008-08-22
  • 打赏
  • 举报
回复
不知道LZ的确切含义
1.0xABCDEF89 转换为:0xEF89ABCD 还是:0x98FEDCBA ?
2.0xABCDEF 转换为:0xCDEF00AB 还是:0xCDEFAB 还是:0xFEDCBA 还是:0xFEDCBA00 ?
ysysbaobei 2008-08-21
  • 打赏
  • 举报
回复
没看懂
bitxinhai 2008-08-21
  • 打赏
  • 举报
回复
使用socket库的函数
u_short htons (u_short hostshort );
u_long ntohl (u_long netlong );
直接实现!!!!
seufl 2008-08-21
  • 打赏
  • 举报
回复
如果是short,则a<<8 | a>>8
其他类型类似
zedzhao 2008-08-21
  • 打赏
  • 举报
回复
学习学习
_石头_ 2008-08-21
  • 打赏
  • 举报
回复

//如果是将int a=0xAABBCCDD 转化成 a=0xDDCCBBAA
int change_order(int value) //参数为需要转换的数据。
{
int i=4; //32为系统的int类型的长度。
char temp[4], *p=(char *)&value; //强制类型转换
while(i--)
temp[i]=*p++;
return *((int *)temp); //返回值位转换后的数据
}
//如果是2字节长度的数据则只需要将i的初始值和temp数组的长度同时改为2即可。

jernymy 2008-08-20
  • 打赏
  • 举报
回复

/* file name : test_reverse.c
* function : to reverse high data to low data
*/

#include "stdio.h" //printf()

int main(void)
{
unsigned int test_int_a = 0xabcdef98;
unsigned int test_int_b;
unsigned int temp_data = 0;

test_int_b = (test_int_a & 0xffff);
test_int_b <<= 16;
temp_data = test_int_a;
temp_data >>= 16;
test_int_b += temp_data;
return 0;
}

#if 0
int main(void)
{
unsigned int test_int_a = 0xabcdef98;
unsigned int test_int_b;
unsigned int temp_data = 0;

test_int_b = (test_int_a & 0xffff);
printf("\na = %x \t", test_int_a);
printf("b = %x \t", test_int_b);
printf("t = %x \t", temp_data);

test_int_b <<= 16;
printf("\na = %x \t", test_int_a);
printf("b = %x \t", test_int_b);
printf("t = %x \t", temp_data);

temp_data = test_int_a;
temp_data >>= 16;
printf("\na = %x \t", test_int_a);
printf("b = %x \t", test_int_b);
printf("t = %x \t", temp_data);

test_int_b += temp_data;
printf("\na = %x \t", test_int_a);
printf("b = %x \t", test_int_b);
printf("t = %x \t", temp_data);
return 0;
}

a = abcdef98 b = ef98 t = 0
a = abcdef98 b = ef980000 t = 0
a = abcdef98 b = ef980000 t = abcd
a = abcdef98 b = ef98abcd t = abcd

#endif



长安宁 2008-08-20
  • 打赏
  • 举报
回复
高低字节转换
高字节&=0xff00>>8
低字节&=0x00ff<<8
低字节&高字节就翻转过来了
bitxinhai 2008-08-20
  • 打赏
  • 举报
回复
unsinged int a = 10,
unsinged int tmp1,tmp2;
tmp1 = (a>>16)& 0xFFFF;
tmp2 = (a<<16)& 0xFFFF0000;

a = tmp1 | tmp2;
cang0lang 2008-08-20
  • 打赏
  • 举报
回复

输入长度和指针

void MySwapWord( int length, void * wordP )
{
int i;
UCHAR temp;

for( i=0; i < length/2; i++ )
{
temp = ((UCHAR *) wordP)[i];
((UCHAR *)wordP)[i] = ((UCHAR *) wordP)[length-i-1];
((UCHAR *) wordP)[length-i-1] = temp;
}
}
lin_style 2008-08-20
  • 打赏
  • 举报
回复
1、用&掩码方式
2、如果是32位,可以申明个16位的指针,指后进行转换。
Zark 2008-08-20
  • 打赏
  • 举报
回复
不知道楼主的要求,猜想是想实现如下:

int a=0xAABBCCDD 转化成 a=0xDDCCBBAA.
short b=0xEEFF 转化成 b=0xFFEE.

方法:
int a=0xAABBCCDD;
unsigned int x=a;
x= (x>>24)+((x&0x00FF0000)>>8)+((x&0x0000FF00)<<8)+((x&0x000000FF)<<24);
a=x;

short b=0xEEFF;
unsigned short y=b;
y= ((y&0x00FF)<<8)+((y&0xFF00)>>8);
b=y;
ysysbaobei 2008-08-20
  • 打赏
  • 举报
回复
mark
kudosky 2008-08-19
  • 打赏
  • 举报
回复
但是我读取的数据是从高到低写的,必须要转换的!
勇敢的天牛 2008-08-19
  • 打赏
  • 举报
回复
c语言是小端对其,不用转的
idle_kitt_heaven 2008-08-19
  • 打赏
  • 举报
回复
#include "stdio.h"
main()
{
int num,number=0,temp,mask,i,j;
printf("Input a integer number:");
scanf("%d",&num);
printf("%d\n",num);
mask=1<<15;
printf("%d=",num);
for(i=1,j=15;i<=16;i++,j--)
{
putchar(num&mask?'1':'0');
temp=(num&mask)>>j;
number=number+temp;
num<<=1;
if(i%4==0)
putchar(',');
}
printf("\bB\n");
printf("高低位转换后输出number=%d\n",number);
printf("高低位转换后输出number=%x\n",number);
}

70,021

社区成员

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

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