C# 使用C读取原始图像文件时保留最后八个字节#

C# 使用C读取原始图像文件时保留最后八个字节#,c#,C#,我试图用C#读取原始图像文件,并将最后2个字节和最后8个字节保留在一个变量中。但是,我的if语句中有一些错误,因此变量只是附加了。。像这样: twoBytes= eightBytes= twoBytes=00 eightBytes=00 twoBytes=0000 eightBytes=0000 twoBytes=000000 eightBytes=000000 twoBytes=00000000 eightBytes=00000000 twoBytes=0000000000 eightByte

我试图用C#读取原始图像文件,并将最后2个字节和最后8个字节保留在一个变量中。但是,我的if语句中有一些错误,因此变量只是附加了。。像这样:

twoBytes= eightBytes=
twoBytes=00 eightBytes=00
twoBytes=0000 eightBytes=0000
twoBytes=000000 eightBytes=000000
twoBytes=00000000 eightBytes=00000000
twoBytes=0000000000 eightBytes=0000000000
twoBytes=000000000000 eightBytes=000000000000
twoBytes=00000000000000 eightBytes=00000000000000
twoBytes=0000000000000000 eightBytes=0000000000000000
twoBytes=000000000000000000 eightBytes=000000000000000000
twoBytes=00000000000000000000 eightBytes=00000000000000000000
twoBytes=0000000000000000000000 eightBytes=0000000000000000000000
twoBytes=000000000000000000000000 eightBytes=000000000000000000000000
twoBytes=00000000000000000000000000 eightBytes=00000000000000000000000000
twoBytes=0000000000000000000000000000 eightBytes=0000000000000000000000000000
twoBytes=000000000000000000000000000000 eightBytes=000000000000000000000000000000
twoBytes=00000000000000000000000000000000 eightBytes=00000000000000000000000000000000
twoBytes=0000000000000000000000000000000000 eightBytes=0000000000000000000000000000000000
twoBytes=000000000000000000000000000000000000 eightBytes=000000000000000000000000000000000000
twoBytes=00000000000000000000000000000000000000 eightBytes=00000000000000000000000000000000000000
我想要类似“twoBytes=55AA”和“eightBytes=”55AA454649205041”的内容

我的代码:

// Read file, byte at the time (example 00, 5A)
FileStream fs = new FileStream("C:\\Users\\user\\image_files\\usb_guid_exfat.001", FileMode.Open);
int hexIn;
String hex;

String twoBytes = "";
String eightBytes = "";

for (int i = 0; (hexIn = fs.ReadByte()) != -1; i++)
{
    hex = string.Format("{0:X2}", hexIn);

    Console.WriteLine("twoBytes=" + twoBytes + " eightBytes=" + eightBytes);
  

    // Transfer two bytes
    twoBytes = twoBytes + hex;
    if (twoBytes.Length < 4)
    {
        if (twoBytes.Length > 6) { 
        twoBytes = twoBytes.Substring(2, 4);
        }
    }

    // Transfer eight bytes
    eightBytes = eightBytes + hex;
    if(eightBytes.Length < 8)
    {
        if (twoBytes.Length > 10) { 
        eightBytes = eightBytes.Substring(2, 8);
        }
    }
}
//读取文件,当时的字节(示例00,5A)
FileStream fs=newfilestream(“C:\\Users\\user\\image\u files\\usb\u guid\u exfat.001”,FileMode.Open);
内六肽;
字符串十六进制;
字符串twoBytes=“”;
字符串八字节=”;
对于(inti=0;(hexIn=fs.ReadByte())!=-1;i++)
{
hex=string.Format(“{0:X2}”,hexIn);
Console.WriteLine(“twoBytes=“+twoBytes+”eightBytes=“+eightBytes”);
//传输两个字节
twoBytes=twoBytes+hex;
if(两字节。长度<4)
{
如果(twoBytes.Length>6){
twoBytes=twoBytes.Substring(2,4);
}
}
//传输八个字节
八字节=八字节+十六进制;
如果(八字节长度<8)
{
如果(twoBytes.Length>10){
八字节=八字节。子串(2,8);
}
}
}

您的if语句是错误的。一个值不能同时小于4和大于6


如果长度是,这能回答你的问题吗?你的if语句没有意义。如果
twoBytes
的长度小于4,则不能同时大于6。正如@HansKilian所说,您的条件没有意义,对于
eightBytes
 twoBytes = twoBytes + hex;
 if (twoBytes.Length > 4) { 
     twoBytes = twoBytes.Substring(twoBytes.Length-4, 4);
 }