ChatGPT解决这个技术问题 Extra ChatGPT

将整数转换为字节数组(Java)

Integer 转换为 Byte Array 的快速方法是什么?

例如0xAABBCCDD => {AA, BB, CC, DD}

结果字节数组的格式是否重要?你会用它做什么?

R
RaphMclee

查看 ByteBuffer 类。

ByteBuffer b = ByteBuffer.allocate(4);
//b.order(ByteOrder.BIG_ENDIAN); // optional, the initial order of a byte buffer is always BIG_ENDIAN.
b.putInt(0xAABBCCDD);

byte[] result = b.array();

设置字节顺序可确保 result[0] == 0xAAresult[1] == 0xBBresult[2] == 0xCCresult[3] == 0xDD

或者,您可以手动执行此操作:

byte[] toBytes(int i)
{
  byte[] result = new byte[4];

  result[0] = (byte) (i >> 24);
  result[1] = (byte) (i >> 16);
  result[2] = (byte) (i >> 8);
  result[3] = (byte) (i /*>> 0*/);

  return result;
}

ByteBuffer 类是为这种脏手任务而设计的。事实上,私有 java.nio.Bits 定义了 ByteBuffer.putInt() 使用的这些辅助方法:

private static byte int3(int x) { return (byte)(x >> 24); }
private static byte int2(int x) { return (byte)(x >> 16); }
private static byte int1(int x) { return (byte)(x >>  8); }
private static byte int0(int x) { return (byte)(x >>  0); }

如果字节缓冲区已经存在,这将工作得很好......否则,分配似乎需要更长的时间,而不是仅仅分配一个长度为 4 的字节数组并手动进行移位......但我们可能在谈论关于小的差异。
ByteBuffer 实例可以被缓存;在内部,无论如何它肯定是通过移位和屏蔽来实现的。
这是一个非常好的答案。请注意,big-endian 是指定的默认值,并且方法是“可链接的”,并且位置参数是可选的,所以它都简化为: byte[] result = ByteBuffer.allocate(4).putInt(0xAABBCCDD).array( );当然,如果您重复执行此操作并将所有结果连接在一起(这在您执行此类操作时很常见),请分配一个缓冲区并重复 putFoo() 将您需要的所有内容放入其中 -它将在您进行时跟踪偏移量。这确实是一个非常有用的课程。
如果您计划在后续的 NIO 写入操作中使用该 ByteBuffer,则需要rewind它。否则,你不会写任何东西! (去过也做过!)
谁不知道。 putInt 将始终写入 4 个字节,无论输入整数的大小是多少。如果您只想要 2 个字节,请使用 putShort 等...
P
Pascal Thivent

使用 BigInteger

private byte[] bigIntToByteArray( final int i ) {
    BigInteger bigInt = BigInteger.valueOf(i);      
    return bigInt.toByteArray();
}

使用 DataOutputStream

private byte[] intToByteArray ( final int i ) throws IOException {      
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    DataOutputStream dos = new DataOutputStream(bos);
    dos.writeInt(i);
    dos.flush();
    return bos.toByteArray();
}

使用 ByteBuffer

public byte[] intToBytes( final int i ) {
    ByteBuffer bb = ByteBuffer.allocate(4); 
    bb.putInt(i); 
    return bb.array();
}

不过要注意字节顺序
ByteBuffer 是否给出无符号整数?
@Pascal 使用 ByteBuffer 我尝试使用 ByteBuffer bb = ByteBuffer.allocate(3);为此,它给出了 java.nio.BufferOverflowException,我不明白为什么它不适用于小于 4 的值?你能解释一下吗?
@SanjayJain 您会收到缓冲区溢出异常,因为 Java 中的 int 大小为 32 位或 4 个字节,因此需要您在 ByteBuffer 中分配至少 4 个字节的内存。
@GregoryPakosz 关于字节顺序是正确的。如果您处理大于 2^31 - 1 的 int,他使用 ByteBuffer 的答案会更直观。
M
Muskovets

使用这个对我有用的功能

public byte[] toByteArray(int value) {
    return new byte[] {
            (byte)(value >> 24),
            (byte)(value >> 16),
            (byte)(value >> 8),
            (byte)value};
}

它将 int 转换为字节值


与其他答案相比,无论最重要的位如何并且更有效,这也毫无价值。也可以使用'>>'。
像这样的直接解决方案肯定比调用任何库方法更快。有时你只需要直接用几行代码来处理这些位,而不是产生库方法调用的所有额外开销。
这可以很好地在语言之间转换,因此有利于多语言软件开发。
P
Pang

如果您喜欢 Guava,您可以使用它的 Ints 类:

对于 intbyte[],使用 toByteArray()

byte[] byteArray = Ints.toByteArray(0xAABBCCDD);

结果是 {0xAA, 0xBB, 0xCC, 0xDD}

它的反面是 fromByteArray()fromBytes()

int intValue = Ints.fromByteArray(new byte[]{(byte) 0xAA, (byte) 0xBB, (byte) 0xCC, (byte) 0xDD});
int intValue = Ints.fromBytes((byte) 0xAA, (byte) 0xBB, (byte) 0xCC, (byte) 0xDD);

结果是 0xAABBCCDD


n
notnoop

您可以使用 BigInteger

从整数:

byte[] array = BigInteger.valueOf(0xAABBCCDD).toByteArray();
System.out.println(Arrays.toString(array))
// --> {-86, -69, -52, -35 }

返回的数组具有表示数字所需的大小,因此它的大小可以是 1,例如表示 1。但是,如果传递了 int,则大小不能超过四个字节。

从字符串:

BigInteger v = new BigInteger("AABBCCDD", 16);
byte[] array = v.toByteArray();

但是,您需要注意,如果第一个字节高于 0x7F(如本例所示),BigInteger 会将一个 0x00 字节插入到数组的开头。这需要区分正值和负值。


谢谢!但由于这是 BigInteger,整数会正确环绕吗?那是在 Integer.MAX_VALUE 之外但仍可以仅用 4 个字节表示的整数?
这当然不会很快执行。 ;)
这不是一个好的选择。它不仅可以添加 0x00 字节,还可以去除前导零。
M
Matt

也可以换档——

byte[] ba = new byte[4];
int val = Integer.MAX_VALUE;

for(byte i=0;i<4;i++)
    ba[i] = (byte)(val >> i*8);
    //ba[3-i] = (byte)(val >> i*8); //Big-endian

C
Community

这是一种可以正确完成工作的方法。

public byte[] toByteArray(int value)
{
    final byte[] destination = new byte[Integer.BYTES];
    for(int index = Integer.BYTES - 1; index >= 0; index--)
    {
        destination[i] = (byte) value;
        value = value >> 8;
    };
    return destination;
};

h
helmy

正确处理 ByteOrder 的简单解决方案:

ByteBuffer.allocate(4).order(ByteOrder.nativeOrder()).putInt(yourInt).array();


M
Mukesh Bambhaniya

用安卓很容易

int i=10000;
byte b1=(byte)Color.alpha(i);
byte b2=(byte)Color.red(i);
byte b3=(byte)Color.green(i);
byte b4=(byte)Color.blue(i);

r
recnac

这将对您有所帮助。

import java.nio.ByteBuffer;
import java.util.Arrays;

public class MyClass
{
    public static void main(String args[]) {
        byte [] hbhbytes = ByteBuffer.allocate(4).putInt(16666666).array();

        System.out.println(Arrays.toString(hbhbytes));
    }
}

M
Muskovets

这是我的解决方案:

public void getBytes(int val) {
    byte[] bytes = new byte[Integer.BYTES];
    for (int i = 0;i < bytes.length; i ++) {
        int j = val % Byte.MAX_VALUE;
        bytes[i] = (j == 0 ? Byte.MAX_VALUE : j);
    }
}

还有 Stringy 方法:

public void getBytes(int val) {
    String hex = Integer.toHexString(val);
    byte[] val = new byte[hex.length()/2]; // because byte is 2 hex chars
    for (int i = 0; i < hex.length(); i+=2)
        val[i] = Byte.parseByte("0x" + hex.substring(i, i+2), 16);
    return val;
}