Java中,int转byte数组

来源:互联网 发布:电脑制作音频软件 编辑:程序博客网 时间:2024/05/21 06:36
private static final int COMMAND_NO_READ_RESOURCE = 0x0224;


HexUtil.intToBytes(COMMAND_NO_READ_RESOURCE, 2)
/** * Transform integer array to byte * @param source *            the source need to be transformed * @param length *            the length of byte array * @return b the length of byte array b */public static byte[] intToBytes(int source, int length) {    byte[] b = new byte[length];    for (int i = 0; i < length; i++) {        b[i] = (byte) (source >> 8 * (length - i - 1) & 0xFF);    }    return b;}

(source >> 8 * (length - i - 1) & 0xFF);
是将int转为2位字节的数组。

参考链接如下:

https://my.oschina.net/u/169390/blog/97495

http://blog.csdn.net/sunnyfans/article/details/8286906


0 0