开发者社区 问答 正文

Java整数到字节数组

我得到一个整数: 1695609641

当我使用方法:

String hex = Integer.toHexString(1695609641); system.out.println(hex); 给出:

6510f329 但我想要一个字节数组:

byte[] bytearray = new byte[] { (byte) 0x65, (byte)0x10, (byte)0xf3, (byte)0x29}; 我该怎么做?

展开
收起
保持可爱mmm 2020-01-16 17:24:12 378 分享 版权
1 条回答
写回答
取消 提交回答
  • 使用Java NIO的ByteBuffer非常简单:

    byte[] bytes = ByteBuffer.allocate(4).putInt(1695609641).array();

    for (byte b : bytes) { System.out.format("0x%x ", b); } 输出:

    0x65 0x10 0xf3 0x29 问题来源于stack overflow

    2020-01-16 17:24:24
    赞同 展开评论
问答分类:
问答标签:
问答地址: