将大端 java 交换为十进制

发布于 2024-10-20 13:12:36 字数 265 浏览 3 评论 0原文

即使在阅读完之后,对整个大端、小端的事情也有点困惑。假设我有一个十六进制值...

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦里°也失望 2024-10-27 13:12:36

请记住,这有时会在末尾打印额外的 00,您可能需要在 hexSwap 中进行修剪。

import java.math.BigInteger;
public class Simon {

    public static String hexSwap(String origHex) {
        // make a number from the hex
        BigInteger orig = new BigInteger(origHex,16);
        // get the bytes to swap
        byte[] origBytes = orig.toByteArray();
        int i = 0;
        while(origBytes[i] == 0) i++;
        // swap the bytes
        byte[] swapBytes = new byte[origBytes.length];
        for(/**/; i < origBytes.length; i++) {
            swapBytes[i] = origBytes[origBytes.length - i - 1];
        }
        BigInteger swap = new BigInteger(swapBytes);
        return swap.toString(10);
    }

    public static void main(String[] args) {
        String orig = "AA37D608DFBFCB01";
        String swap = hexSwap(orig);
        System.out.println(swap);
    }

}

Keep in mind that this will sometimes print extra 00s on the end that you may need to trim in the hexSwap.

import java.math.BigInteger;
public class Simon {

    public static String hexSwap(String origHex) {
        // make a number from the hex
        BigInteger orig = new BigInteger(origHex,16);
        // get the bytes to swap
        byte[] origBytes = orig.toByteArray();
        int i = 0;
        while(origBytes[i] == 0) i++;
        // swap the bytes
        byte[] swapBytes = new byte[origBytes.length];
        for(/**/; i < origBytes.length; i++) {
            swapBytes[i] = origBytes[origBytes.length - i - 1];
        }
        BigInteger swap = new BigInteger(swapBytes);
        return swap.toString(10);
    }

    public static void main(String[] args) {
        String orig = "AA37D608DFBFCB01";
        String swap = hexSwap(orig);
        System.out.println(swap);
    }

}
无敌元气妹 2024-10-27 13:12:36

作为参考,仅查看一堆字节并不能告诉您一个数字是小端还是大端(除非您碰巧已经知道它们代表的数字)。您首先必须关心的原因是,相同的字节代表两个不同的数字,具体取决于字节是否应解释为小端字节序或大端字节序。

话虽如此,如果您知道字节代表一个小端数字,并且您想将其解释为 Java long,您可以使用如下代码:

long result = 0;
for (int i = 0; i < 8; ++i)
{
    result |= (bytes[i] & 0xffL) << (8 * i);
}

一旦您有了它,您就可以使用如果需要,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:

long result = 0;
for (int i = 0; i < 8; ++i)
{
    result |= (bytes[i] & 0xffL) << (8 * i);
}

Once you have that, you can use Long.toString(result, 16) to show the result as a hex number if you want.

暮光沉寂 2024-10-27 13:12:36

如下所述,您可以使用 ByteBuffer< /code>将值解释为大端或小端数量。 此处有一个相关示例。为方便起见,请注意 BigDecimaldouble 构造函数“将 double 转换为 BigDecimal,这是 double 的二进制浮点数的精确十进制表示形式- 点值。”

控制台:

400921fb54442d18 = 3.141592653589793
182d4454fb210940 = 3.207375630676366E-192
3.141592653589793 = 3.141592653589793115997963468544185161590576171875

代码:

import java.math.BigDecimal;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;

/** @see https://stackoverflow.com/questions/5200863 */
public class DoubleOrder {

    public static void main(String[] args) {
        double pi = Math.PI;
        ByteBuffer bb = ByteBuffer.allocate(Double.SIZE / 8);
        bb.putDouble(pi);
        display(bb, ByteOrder.BIG_ENDIAN);
        display(bb, ByteOrder.LITTLE_ENDIAN);
        System.out.println(pi + " = " + new BigDecimal(pi));
    }

    private static void display(ByteBuffer bb, ByteOrder order) {
        bb.order(order);
        Long bits = Double.doubleToLongBits(bb.getDouble(0));
        String s = Long.toHexString(bits);
        Double d = Double.longBitsToDouble(Long.valueOf(s, 16));
        System.out.println(s + " = " + d.toString());
    }
}

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 the double constructor of BigDecimal "Translates a double into a BigDecimal which is the exact decimal representation of the double's binary floating-point value."

Console:

400921fb54442d18 = 3.141592653589793
182d4454fb210940 = 3.207375630676366E-192
3.141592653589793 = 3.141592653589793115997963468544185161590576171875

Code:

import java.math.BigDecimal;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;

/** @see https://stackoverflow.com/questions/5200863 */
public class DoubleOrder {

    public static void main(String[] args) {
        double pi = Math.PI;
        ByteBuffer bb = ByteBuffer.allocate(Double.SIZE / 8);
        bb.putDouble(pi);
        display(bb, ByteOrder.BIG_ENDIAN);
        display(bb, ByteOrder.LITTLE_ENDIAN);
        System.out.println(pi + " = " + new BigDecimal(pi));
    }

    private static void display(ByteBuffer bb, ByteOrder order) {
        bb.order(order);
        Long bits = Double.doubleToLongBits(bb.getDouble(0));
        String s = Long.toHexString(bits);
        Double d = Double.longBitsToDouble(Long.valueOf(s, 16));
        System.out.println(s + " = " + d.toString());
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文