记录一个int[] 转 byte[]的工具方法:

public static byte[] IntArrayToByteArray(int[] intArray) {
        if (intArray == null || intArray.length == 0) {
            return null;
        }

        ByteBuffer byteBuffer = ByteBuffer.allocate(intArray.length * 4);
        byteBuffer.order(ByteOrder.LITTLE_ENDIAN);

        IntBuffer intBuffer = byteBuffer.asIntBuffer();
        intBuffer.put(intArray);

        return byteBuffer.array();
    }

例:

int[] arr = {0x12345678, 0x00ABCDEF};

调用上述方法转换完成的结果为:

78 56 34 12 EF CD AB 00 

 

PS: 我这里使用的是小端格式,如果想要得到大端格式的结果,需要将红色code 修改成:  byteBuffer.order(ByteOrder.BIG_ENDIAN); 

over

相关文章:

  • 2022-12-23
  • 2021-12-16
  • 2021-12-03
  • 2022-12-23
  • 2022-02-05
  • 2021-08-06
  • 2022-02-19
猜你喜欢
  • 2022-12-23
  • 2021-12-19
  • 2021-09-05
  • 2022-12-23
  • 2022-12-23
  • 2021-11-28
  • 2022-12-23
相关资源
相似解决方案