将小端转换为大端

发布于 2024-09-25 10:40:29 字数 413 浏览 0 评论 0原文

所有,

我一直在网上练习编码问题。目前我正在研究一个问题陈述 问题,我们需要转换 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 技术交流群。

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

发布评论

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

评论(7

愛上了 2024-10-02 10:40:29

由于编写软件的很大一部分是重用现有的解决方案,因此第一件事应该始终是查看您的语言/库的文档。

reverse = Integer.reverseBytes(x);

我不知道这个函数的效率如何,但是对于切换大量数字,ByteBuffer 应该提供不错的性能。

import java.nio.ByteBuffer;
import java.nio.ByteOrder;

...

int[] myArray = aFountOfIntegers();
ByteBuffer buffer = ByteBuffer.allocate(myArray.length*Integer.BYTES);

buffer.order(ByteOrder.LITTLE_ENDIAN);
for (int x:myArray) buffer.putInt(x);

buffer.order(ByteOrder.BIG_ENDIAN);
buffer.rewind();
int i=0;
for (int x:myArray) myArray[i++] = buffer.getInt(x);

正如eversor在评论中指出的那样,ByteBuffer.putInt()是一个可选方法,并且可能不适用于所有Java实现。

DIY Approach

Stacker 的答案非常简洁,但可以对其进行改进。

   reversed = (i&0xff)<<24 | (i&0xff00)<<8 | (i&0xff0000)>>8 | (i>>24)&0xff;

我们可以通过调整位掩码来去掉括号。例如,(a & 0xFF)<<8 相当于 a<<8 & 0xFF00。无论如何,最右边的括号是不必要的。

   reversed = i<<24 & 0xff000000 | i<<8 & 0xff0000 | i>>8 & 0xff00 | i>>24 & 0xff;

由于左移移入零位,因此第一个掩码是多余的。我们可以通过使用逻辑移位运算符来去掉最右边的掩码,该运算符仅移位零位。

   reversed = i<<24 | i>>8 & 0xff00 | i<<8 & 0xff0000 | i>>>24;

运算符优先级 这里,有关移位运算符的详细信息位于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.

reverse = Integer.reverseBytes(x);

I don't know how efficient this function is, but for toggling lots of numbers, a ByteBuffer should offer decent performance.

import java.nio.ByteBuffer;
import java.nio.ByteOrder;

...

int[] myArray = aFountOfIntegers();
ByteBuffer buffer = ByteBuffer.allocate(myArray.length*Integer.BYTES);

buffer.order(ByteOrder.LITTLE_ENDIAN);
for (int x:myArray) buffer.putInt(x);

buffer.order(ByteOrder.BIG_ENDIAN);
buffer.rewind();
int i=0;
for (int x:myArray) myArray[i++] = buffer.getInt(x);

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.

   reversed = (i&0xff)<<24 | (i&0xff00)<<8 | (i&0xff0000)>>8 | (i>>24)&0xff;

We can get rid of the parentheses by adapting the bitmasks. E. g., (a & 0xFF)<<8 is equivalent to a<<8 & 0xFF00. The rightmost parentheses were not necessary anyway.

   reversed = i<<24 & 0xff000000 | i<<8 & 0xff0000 | i>>8 & 0xff00 | i>>24 & 0xff;

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.

   reversed = i<<24 | i>>8 & 0xff00 | i<<8 & 0xff0000 | i>>>24;

Operator precedence here, the gritty details on shift operators are in the Java Language Specification

寄居人 2024-10-02 10:40:29

看看这个

int little2big(int i) {
    return (i&0xff)<<24 | (i&0xff00)<<8 | (i&0xff0000)>>8 | (i>>24)&0xff;
}

Check this out

int little2big(int i) {
    return (i&0xff)<<24 | (i&0xff00)<<8 | (i&0xff0000)>>8 | (i>>24)&0xff;
}
胡渣熟男 2024-10-02 10:40:29

您需要意识到的是,字节序交换处理表示整数的字节。所以 4 字节数字 27 看起来像 0x0000001B。要转换该数字,需要转到 0x1B000000...在您的示例中,123456789 的十六进制表示是 0x075BCD15 ,需要转到 0x15CD5B07 code> 或十进制形式 365779719。

Stacker 发布的函数通过位移位来移动这些字节;更具体地说,语句 i&0xffi 中获取最低字节,<< 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 to 0x1B000000... With your example, the hex representation of 123456789 is 0x075BCD15 which needs to go to 0x15CD5B07 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 from i, 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.

躲猫猫 2024-10-02 10:40:29

Java 原始包装类从 1.5 开始使用 reverseBytes 方法支持字节反转。

Short.reverseBytes(short i)
Integer.reverseBytes(int i)
Long.reverseBytes(long i)

这只是对那些在 2018 年寻找这个答案的人的贡献。

Java primitive wrapper classes support byte reversing since 1.5 using reverseBytes method.

Short.reverseBytes(short i)
Integer.reverseBytes(int i)
Long.reverseBytes(long i)

Just a contribution for those who are looking for this answer in 2018.

彼岸花似海 2024-10-02 10:40:29

我认为这也可以有所帮助:

int littleToBig(int i)
{
    int b0,b1,b2,b3;

    b0 = (i&0x000000ff)>>0;
    b1 = (i&0x0000ff00)>>8;
    b2 = (i&0x00ff0000)>>16;
    b3 = (i&0xff000000)>>24;

    return ((b0<<24)|(b1<<16)|(b2<<8)|(b3<<0));
}

I think this can also help:

int littleToBig(int i)
{
    int b0,b1,b2,b3;

    b0 = (i&0x000000ff)>>0;
    b1 = (i&0x0000ff00)>>8;
    b2 = (i&0x00ff0000)>>16;
    b3 = (i&0xff000000)>>24;

    return ((b0<<24)|(b1<<16)|(b2<<8)|(b3<<0));
}
空宴 2024-10-02 10:40:29

只需使用 java 中的静态函数(reverseBytes(int i)),该函数位于 Integer Wrapper 类

Integer i=Integer.reverseBytes(123456789);
System.out.println(i);

输出下:

365779719

Just use the static function (reverseBytes(int i)) in java which is under Integer Wrapper class

Integer i=Integer.reverseBytes(123456789);
System.out.println(i);

output:

365779719
春庭雪 2024-10-02 10:40:29

以下方法反转字节值中的位顺序:

public static byte reverseBitOrder(byte b) {
    int converted = 0x00;
    converted ^= (b & 0b1000_0000) >> 7;
    converted ^= (b & 0b0100_0000) >> 5;
    converted ^= (b & 0b0010_0000) >> 3;
    converted ^= (b & 0b0001_0000) >> 1;
    converted ^= (b & 0b0000_1000) << 1;
    converted ^= (b & 0b0000_0100) << 3;
    converted ^= (b & 0b0000_0010) << 5;
    converted ^= (b & 0b0000_0001) << 7;

    return (byte) (converted & 0xFF);
}

the following method reverses the order of bits in a byte value:

public static byte reverseBitOrder(byte b) {
    int converted = 0x00;
    converted ^= (b & 0b1000_0000) >> 7;
    converted ^= (b & 0b0100_0000) >> 5;
    converted ^= (b & 0b0010_0000) >> 3;
    converted ^= (b & 0b0001_0000) >> 1;
    converted ^= (b & 0b0000_1000) << 1;
    converted ^= (b & 0b0000_0100) << 3;
    converted ^= (b & 0b0000_0010) << 5;
    converted ^= (b & 0b0000_0001) << 7;

    return (byte) (converted & 0xFF);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文