将小端转换为大端
所有,
我一直在网上练习编码问题。目前我正在研究一个问题陈述 问题,我们需要转换 Big Endian <- >小尾数法。但考虑到给出的示例,我无法记下步骤:
123456789 converts to 365779719
我正在考虑的逻辑是:
1>获取整数值(由于我在Windows x86上,所以输入是Little endian)
2>生成相同的十六进制表示。
3>反转表示并生成大端整数值
但我显然在这里遗漏了一些东西。
任何人都可以指导我吗?我正在使用 Java 1.5 编写代码
All,
I have been practicing coding problems online. Currently I am working on a problem statement Problems where we need to convert Big Endian <-> little endian. But I am not able to jot down the steps considering the example given as:
123456789 converts to 365779719
The logic I am considering is :
1 > Get the integer value (Since I am on Windows x86, the input is Little endian)
2 > Generate the hex representation of the same.
3 > Reverse the representation and generate the big endian integer value
But I am obviously missing something here.
Can anyone please guide me. I am coding in Java 1.5
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
由于编写软件的很大一部分是重用现有的解决方案,因此第一件事应该始终是查看您的语言/库的文档。
我不知道这个函数的效率如何,但是对于切换大量数字,
ByteBuffer
应该提供不错的性能。正如eversor在评论中指出的那样,ByteBuffer.putInt()是一个可选方法,并且可能不适用于所有Java实现。
DIY Approach
Stacker 的答案非常简洁,但可以对其进行改进。
我们可以通过调整位掩码来去掉括号。例如,
(a & 0xFF)<<8
相当于a<<8 & 0xFF00
。无论如何,最右边的括号是不必要的。由于左移移入零位,因此第一个掩码是多余的。我们可以通过使用逻辑移位运算符来去掉最右边的掩码,该运算符仅移位零位。
运算符优先级 这里,有关移位运算符的详细信息位于Java 语言规范
Since a great part of writing software is about reusing existing solutions, the first thing should always be a look into the documentation for your language/library.
I don't know how efficient this function is, but for toggling lots of numbers, a
ByteBuffer
should offer decent performance.As eversor pointed out in the comments,
ByteBuffer.putInt()
is an optional method, and may not be available on all Java implementations.The DIY Approach
Stacker's answer is pretty neat, but it is possible to improve upon it.
We can get rid of the parentheses by adapting the bitmasks. E. g.,
(a & 0xFF)<<8
is equivalent toa<<8 & 0xFF00
. The rightmost parentheses were not necessary anyway.Since the left shift shifts in zero bits, the first mask is redundant. We can get rid of the rightmost mask by using the logical shift operator, which shifts in only zero bits.
Operator precedence here, the gritty details on shift operators are in the Java Language Specification
看看这个
Check this out
您需要意识到的是,字节序交换处理表示整数的字节。所以 4 字节数字 27 看起来像
0x0000001B
。要转换该数字,需要转到0x1B000000
...在您的示例中,123456789 的十六进制表示是0x075BCD15
,需要转到0x15CD5B07
code> 或十进制形式 365779719。Stacker 发布的函数通过位移位来移动这些字节;更具体地说,语句
i&0xff
从i
中获取最低字节,<< 24
然后将其上移 24 位,即从位置 1-8 到 25-32。依此类推,完成表达式的每个部分。有关示例代码,请查看此实用程序。
The thing you need to realize is that endian swaps deal with the bytes that represent the integer. So the 4 byte number 27 looks like
0x0000001B
. To convert that number, it needs to go to0x1B000000
... With your example, the hex representation of 123456789 is0x075BCD15
which needs to go to0x15CD5B07
or in decimal form 365779719.The function Stacker posted is moving those bytes around by bit shifting them; more specifically, the statement
i&0xff
takes the lowest byte fromi
, the<< 24
then moves it up 24 bits, so from positions 1-8 to 25-32. So on through each part of the expression.For example code, take a look at this utility.
Java 原始包装类从 1.5 开始使用
reverseBytes
方法支持字节反转。这只是对那些在 2018 年寻找这个答案的人的贡献。
Java primitive wrapper classes support byte reversing since 1.5 using
reverseBytes
method.Just a contribution for those who are looking for this answer in 2018.
我认为这也可以有所帮助:
I think this can also help:
只需使用 java 中的静态函数(reverseBytes(int i)),该函数位于 Integer Wrapper 类
输出下:
Just use the static function (reverseBytes(int i)) in java which is under Integer Wrapper class
output:
以下方法反转字节值中的位顺序:
the following method reverses the order of bits in a byte value: