交换整数字节 [英] swapping bytes in an integer

查看:76
本文介绍了交换整数字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我的32位无符号整数是错误的字节顺序,那么我可以如何转换它?b $ b?例如,如果有一个数字0x090a0b0c我怎么能将
反转为0x0c0b0a09?


谢谢,

-John

解决方案

2006年5月8日14:03:08 -0700, jo ** @ fcs.uga.edu 写道:

如果我有一个32位无符号整数,这是一个错误的字节顺序,如何
我可以转换它吗?例如,如果有一个数字0x090a0b0c我怎么能将其反转为0x0c0b0a09?

谢谢,
-John




移位运算符<<和>>左右移一个值,分别为
。移位4位会将值移动一个十六进制数字。

您可以将值与按位OR运算符组合,|


jo ** @ fcs.uga.edu 写道:

如果我有一个32位无符号整数错误的字节顺序,我怎么转换它?例如,如果有一个数字0x090a0b0c我怎么能将其反转为0x0c0b0a09?



如果你的系统有网络库那么我是非标准的>
功能ntohl,你不需要携带,你可以使用它。


-

Ian Collins。


猴子猴= 0x090a0b0c;



Opps!应该写的:

猴子猴子;


monkey.four_bytes = 0x090a0b0c;

-Tomás


If I have a 32 bit unsigned int that is in the wrong byte order, how
can I convert it? For example, if have a number 0x090a0b0c how can I
reverse this to 0x0c0b0a09 ?

Thanks,
-John

解决方案

On 8 May 2006 14:03:08 -0700, jo**@fcs.uga.edu wrote:

If I have a 32 bit unsigned int that is in the wrong byte order, how
can I convert it? For example, if have a number 0x090a0b0c how can I
reverse this to 0x0c0b0a09 ?

Thanks,
-John



The shift operators << and >> shift a value left and right by one bit,
respectively. Shifting by 4 bits will move the value by one hex digit.
You can combine values with the bitwise OR operator, |


jo**@fcs.uga.edu wrote:

If I have a 32 bit unsigned int that is in the wrong byte order, how
can I convert it? For example, if have a number 0x090a0b0c how can I
reverse this to 0x0c0b0a09 ?


It my be non-standard, bit if you system has the networking library
function ntohl and you don''t require portability, you can use that.

--
Ian Collins.


Monkey monkey = 0x090a0b0c;


Opps! Should have written:
Monkey monkey;

monkey.four_bytes = 0x090a0b0c;
-Tomás


这篇关于交换整数字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆