尝试读取数据时出现奇怪的问题

时间:2009-11-07 18:21:25

标签: c# long-integer binaryreader

当我写:

var tagType = _reader.ReadByte();

while (tagType != 8)
{
    var skip = ReadNext3Bytes() + 11;
    _reader.BaseStream.Position += skip;

    tagType = _reader.ReadByte();
}

......它正在发挥作用,但是当我写作时:

var tagType = _reader.ReadByte();

while (tagType != 8)
{
    _reader.BaseStream.Position += ReadNext3Bytes() + 11; 
     tagType = _reader.ReadByte();
}

......它不起作用,我无法理解为什么 - 我得到了意想不到的结果。继承人ReadNext3Bytes方法:

    private long ReadNext3Bytes()
    {
        try
        {
            return Math.Abs((_reader.ReadByte() & 0xFF) * 256 * 256 + (_reader.ReadByte() & 0xFF) 
                * 256 + (_reader.ReadByte() & 0xFF));
        }
        catch 
        {
            return 0;
        }
    }

为什么会这样,我该如何解决?

感谢。

1 个答案:

答案 0 :(得分:2)

ReadByte 调用期间更改了 Position 会发生这种情况,您看到的内容类似于这种情况:

int position = 1;
position += (position = 2) + 3;

Assert.AreEqual(6, position);