int i = 5;
    ByteArrayOutputStream buf = new ByteArrayOutputStream();
    DataOutputStream out = new DataOutputStream(buf);
    out.writeInt(i);
    byte[] b = buf.toByteArray();

解决方案 »

  1.   

    int i = 5;
    String s = String.valueOf(i);
    byte[] b = s.getBytes();
      

  2.   

    谢谢,不过 sundayfleet(sunday) 的做法肯定是不对的。
    int 5 对应的二进制代码05h
    而字符5的二进制代码为35h
      

  3.   

    public static byte[] IntToByteArray(int i) {
      byte[] result = new byte[4];
    result[0] = (byte)(i & 0xFF);
    result[1] = (byte)((i >> 8) & 0xFF);
    result[2] = (byte)((i >> 16) & 0xFF);
    result[3] = (byte)((i >> 24) & 0xFF);
    return result;
    }
      

  4.   

    whywzf(古风)的方法要好点,
    速度也快.
    第一个人用到了io,当然不好.
    第二个人用错了.