给你一个函数。需要j2sdk1.4 中的 java.nio.*
如果一定好j2sdk1.3,也可以用流代替ByteBuffer    public static void int2bytes(int in, byte[] dst, int offset) {
        ByteBuffer bb = ByteBuffer.allocate(4);
        bb.clear();
        bb.putShort(in);
        bb.flip();
        bb.get(dst, offset, 4);
    }

解决方案 »

  1.   

    sorry 
    bb.putShort(in);
    应该是bb.putInt(in);
      

  2.   

    public  static byte[] integerToBytes(int intValue)
        {
            byte[] byteValue = new byte[4];
            int shift = 0;
            for (int x = 0; x < 4; x++)
            {
                shift -= 8;
                byteValue[x] = (byte)(intValue >>> shift);
            }
            return byteValue;
        }
      

  3.   

    public static int BytesToInteger(byte[] byteValues) throws Exception
        {
            int intValue = 0;
            //如果byte数组长度不足4位,则抛出异常
            if (byteValues.length <4 )
            {
                throw new Exception("传入的byte数组不足4位");
            }
            //只取Bytes的前4位
            for (int x = 0; x < 4; x++)
            {
                intValue <<= 8;
                intValue |= byteValues[x] & 0xFF;
            }
            return intValue;
        }