将大端 java 交换为十进制
即使在阅读完之后,对整个大端、小端的事情也有点困惑。假设我有一个十六进制值...
AA 37 D6 08 DF BF CB 01
并且我想将其转换为...
01 CB BF DF 08 D6 37 AA
(这是大端对吗?)我将如何在Java中执行此操作?一旦完成,我需要将其转换为十进制。抱歉,如果这是直截了当的,我对此很陌生,因为我的头开始旋转,哈哈。感谢您提前提供任何帮助!
Little confused on the whole big endian, little endian thing even after reading up on it. Say I have a Hex value of...
AA 37 D6 08 DF BF CB 01
and I want to convert it to...
01 CB BF DF 08 D6 37 AA
(Which is big endian right?) How would I do this in Java? Once thats done I would need to convert it to decimal. Sorry if this is straight forward I'm quite new to this as my head is spinning from it lol. Thanks for any help in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
请记住,这有时会在末尾打印额外的 00,您可能需要在 hexSwap 中进行修剪。
Keep in mind that this will sometimes print extra 00s on the end that you may need to trim in the hexSwap.
作为参考,仅查看一堆字节并不能告诉您一个数字是小端还是大端(除非您碰巧已经知道它们代表的数字)。您首先必须关心的原因是,相同的字节代表两个不同的数字,具体取决于字节是否应解释为小端字节序或大端字节序。
话虽如此,如果您知道字节代表一个小端数字,并且您想将其解释为 Java
long
,您可以使用如下代码:一旦您有了它,您就可以使用如果需要,
Long.toString(result, 16)
将结果显示为十六进制数字。For reference, just looking at a bunch of bytes won't tell you whether a number is little-endian or big-endian ( unless you happen to already know the number they represent). The reason you have to care in the first place, is that the same bytes represent two different numbers depending on whether the bytes should be interpreted as little-endian or big-endian.
Now that that's said, if you know the bytes represent a little-endian number, and you want to interpret it as a Java
long
, you could use some code like:Once you have that, you can use
Long.toString(result, 16)
to show the result as a hex number if you want.如下所述,您可以使用
ByteBuffer< /code>
将值解释为大端或小端数量。 此处有一个相关示例。为方便起见,请注意
BigDecimal
的double
构造函数“将 double 转换为BigDecimal
,这是 double 的二进制浮点数的精确十进制表示形式- 点值。”控制台:
代码:
As outlined below, you can use
ByteBuffer
to interpret a value as either a big- or little-endian quantity. There's a related example here. As a convenience, note that thedouble
constructor ofBigDecimal
"Translates a double into aBigDecimal
which is the exact decimal representation of the double's binary floating-point value."Console:
Code: