BinaryReader 跳过位置
BinaryReader Skipping Ahead In Position
下面是我的 BinaryReader 循环,我尝试逐步查看发生了什么,但是当它返回设置 Position 变量时,它从“433939”变为“433941”,没有其他交互有了它,它在整个循环中都没有改变,而是跳过它而不是从 433939 到 433940。我在这里遗漏了什么吗?
while (Binary.BaseStream.Position < Binary.BaseStream.Length)
{
CurrentData = Binary.ReadByte().ToString("X2");
Position = Binary.BaseStream.Position;
if (CurrentData == "FF")
{
if (ImageFound == false)
{
if (Binary.ReadByte().ToString("X2") == "D8")
{
Console.WriteLine("Header Found!");
ImageFound = true;
Binary.BaseStream.Position = Position;
continue;
}
}
if (ImageFound == true)
{
if (Binary.ReadByte().ToString("X2") == "D9")
{
Console.WriteLine("Footer Fount!");
ImageFound = false;
Image.Append(Binary.ReadByte().ToString("FFD9"));
File.WriteAllText(new Random().Next().ToString() + ".bin", Image.ToString());
Console.ReadLine();
continue;
}
}
}
if (ImageFound == true)
{
Image.Append(CurrentData);
continue;
}
}
我真的看不出这是哪里出了问题,就像我说的跨步或断点根本没有帮助,这不是很好。
哦,我觉得很愚蠢,我必须休息一下或喝杯咖啡。问题是我正在推进位置进行比较,如果失败则不重置它。
if (Binary.ReadByte().ToString("X2") == "D9")
将它推进一个字节并返回 false,所以我只需要添加,否则说
Binary.BaseStream.Position--;
下面是我的 BinaryReader 循环,我尝试逐步查看发生了什么,但是当它返回设置 Position 变量时,它从“433939”变为“433941”,没有其他交互有了它,它在整个循环中都没有改变,而是跳过它而不是从 433939 到 433940。我在这里遗漏了什么吗?
while (Binary.BaseStream.Position < Binary.BaseStream.Length)
{
CurrentData = Binary.ReadByte().ToString("X2");
Position = Binary.BaseStream.Position;
if (CurrentData == "FF")
{
if (ImageFound == false)
{
if (Binary.ReadByte().ToString("X2") == "D8")
{
Console.WriteLine("Header Found!");
ImageFound = true;
Binary.BaseStream.Position = Position;
continue;
}
}
if (ImageFound == true)
{
if (Binary.ReadByte().ToString("X2") == "D9")
{
Console.WriteLine("Footer Fount!");
ImageFound = false;
Image.Append(Binary.ReadByte().ToString("FFD9"));
File.WriteAllText(new Random().Next().ToString() + ".bin", Image.ToString());
Console.ReadLine();
continue;
}
}
}
if (ImageFound == true)
{
Image.Append(CurrentData);
continue;
}
}
我真的看不出这是哪里出了问题,就像我说的跨步或断点根本没有帮助,这不是很好。
哦,我觉得很愚蠢,我必须休息一下或喝杯咖啡。问题是我正在推进位置进行比较,如果失败则不重置它。
if (Binary.ReadByte().ToString("X2") == "D9")
将它推进一个字节并返回 false,所以我只需要添加,否则说
Binary.BaseStream.Position--;