【问题标题】:Convert integer into byte array (Java)将整数转换为字节数组(Java)
【发布时间】:2010-12-28 13:19:39
【问题描述】:

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

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

【问题讨论】:

  • 生成的字节数组的格式是否重要?你会用它做什么?

标签: java arrays integer byte


【解决方案1】:

看看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); }

【讨论】:

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

使用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 的回答会更直观。
【解决方案3】:

使用这个对我有用的功能

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

它将 int 转换为字节值

【讨论】:

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

如果你喜欢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

【讨论】:

    【解决方案5】:

    你可以使用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 字节,还可以去除前导零。
    【解决方案6】:

    也可以换档-

    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
    

    【讨论】:

      【解决方案7】:

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

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

      【讨论】:

        【解决方案8】:

        这会对你有所帮助。

        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));
            }
        }
        

        【讨论】:

          【解决方案9】:

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

          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;
          };
          

          【讨论】:

            【解决方案10】:

            这是我的解决方案:

            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;
            }
            

            【讨论】:

              【解决方案11】:

              用安卓很容易

              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);
              

              【讨论】:

                猜你喜欢
                • 2018-11-19
                • 1970-01-01
                • 2010-11-05
                • 2011-07-02
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多