public static void init (InputStream inputStream) throws Exception 
 {
     ObjectInputStream in = new ObjectInputStream(inputStream);
     try {
     int i = 0;
     while(in.available() > 0) {
     .....
     X[i >> 1] = in.readInt();
     i++;
                                ......
     }
     }
                finally {
     if(in != null)
     in.close();
     }
    }
    Java代码是读取一个dat流文件,用ObjectInputStream类的readInt()函数取值,我看ObjectInputStream类的readInt()的说明是读取一个32位的整数,并且将流的当前位置后移16位,是这样理解吗?
    但是C#里用BinaryReader.ReadInt32(),读取的结果却是错的?那如何用C#代码实现这个功能哩?
    谢谢大家。Java ObjectInputStreamreadInt()

解决方案 »

  1.   

    读============================
    是不是的测试一下吧,看C和JAVA的高低位是不是相同的。
      

  2.   

    这是JAVA中的源代码。
    public int readInt() throws IOException {
        if (!blkmode) {
    pos = 0;
    in.readFully(buf, 0, 4);
        } else if (end - pos < 4) {
    return din.readInt();
        }
        int v = Bits.getInt(buf, pos);
        pos += 4;
        return v;
    }
    读到4个位,的确是32位。
    static int getInt(byte[] b, int off) {
    return ((b[off + 3] & 0xFF) << 0) +
           ((b[off + 2] & 0xFF) << 8) +
           ((b[off + 1] & 0xFF) << 16) +
           ((b[off + 0]) << 24);
        }
    第0个字节是高位,最后一个字节是低位,C#中的,就需要楼主自己去研究的。