将整数转换为长度为4的字节数组。
//package com.nowjava; // 来 自 时 代 J a v a 公 众 号 - nowjava.com public class Main { public static void main(String[] argv) throws Exception { int a = 2; byte[] buf = new byte[] { 34, 35, 36, 37, 37, 37, 67, 68, 69 }; integerToByteArray(a, buf); } /** * Converts an integer to a byte array with a length of 4. * @param a - The integer to convert. * @param buf - The byte array to store the integer in. This must be a length of at least 4. */ public static void integerToByteArray(int a, byte[] buf) { buf[0] = (byte) ((a >> 24) & 0xFF); buf[1] = (byte) ((a &