如题。

解决方案 »

  1.   

    int i = 256;
    byte[] bytes = new byte[4];
    bytes[0] = (byte)(i & 0XFF);
    bytes[1] = (byte)(i >>> 8 & 0XFF);
    bytes[2] = (byte)(i >>> 16& 0XFF);
    bytes[3] = (byte)(i >>> 24 & 0XFF);
      

  2.   

    在java中,int不是4个字节长嘛?楼上的代码的意思就是按字节位取这4个字节嘛.
      

  3.   


    int intNum = 2;
      String strInt = String.format("%04d", intNum);
      System.out.println(strInt);输出0002
      

  4.   

    用位运算 分别取出INT的4个字节 然后放到BYTE[]里
    就可以了!
      

  5.   


    public class Bit {
    public static void main(String args[]){
    int a = 10000;
    int c = 0xFF;
    byte b[] = new byte[4];
    b[0] = (byte) (a & c);
    b[1] = (byte) ((a >> 8) & c);
    b[2] = (byte) ((a >> 16 )& c);
    b[3] = (byte) ((a >> 24) & c);

    for(int i = 3; i >= 0;i--){
    System.out.print(b[i]+" ");
    }
      }
    }
      

  6.   


    public byte[] D2B(int num) {
    // 用于获取后2字节的掩码
    int mask = 0xFF;// 保存转换字节数组
    byte b[] = new byte[4];// 转换每2字节的结果
    for(int i = 0; i < 4; i ++) {
     b[i] = (byte)((num >> i * 8) & mask);
    }return b;
    }