Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从文件读取十六进制数据,从位置x开始,在第一个空字节(00)结束?_C#_.net_C# 4.0_Hex_Filereader - Fatal编程技术网

C# 从文件读取十六进制数据,从位置x开始,在第一个空字节(00)结束?

C# 从文件读取十六进制数据,从位置x开始,在第一个空字节(00)结束?,c#,.net,c#-4.0,hex,filereader,C#,.net,C# 4.0,Hex,Filereader,所以我需要读一个文件,里面有一个十六进制的单词(ACSII)。当然,单词可以是任意长度,但总是从偏移量0x1290开始。我试图做的是读取从偏移量0x1290开始的文件的十六进制,然后继续读取,直到遇到空字节(00)。到目前为止,我尝试过的所有编码似乎都需要一个固定的长度来读取 filePath = "C:\myfile" BinaryReader reader = new BinaryReader(new FileStream(filePath, FileMode.Open, FileAcces

所以我需要读一个文件,里面有一个十六进制的单词(ACSII)。当然,单词可以是任意长度,但总是从偏移量0x1290开始。我试图做的是读取从偏移量0x1290开始的文件的十六进制,然后继续读取,直到遇到空字节(00)。到目前为止,我尝试过的所有编码似乎都需要一个固定的长度来读取

filePath = "C:\myfile"
BinaryReader reader = new BinaryReader(new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.None));
reader.BaseStream.Position = 0x1290;     // The starting offset
byte[] word = reader.ReadBytes(); // Must specify length within ReadBytes(e.g. 0x99)
reader.Close();`
在所需的“字”之后,通常可以有其他不需要的十六进制数据,但是在“字”之后总是有一个空字节。这就是我无法指定长度的原因

使用while循环:

filePath = "C:\myfile"
BinaryReader reader = new BinaryReader(new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.None));
reader.BaseStream.Position = 0x1290;
byte char;
byte[] result=new byte[length_of_the_word];
int i=0;

while(((byte)char=reader.Read())!=-1)
{
   result[i++]=char;
}

reader.close();

然后用结束字十六进制替换-1(文件结尾),比如0x00,如果您不知道字符串的长度是多少个字符,那么应该使用某种类型的集合,而不需要事先存储数据的确切长度。这似乎是
列表的完美工作

列表位=新列表();
使用(filestreams=newfilestream(@“C:\myfile”,FileMode.Open))
使用(BinaryReader=新的BinaryReader))
{
reader.BaseStream.Position=0x1290;
while(reader.PeekChar()!=-1)
{
字节b=读取器.ReadByte();
如果(b!=0x00)
Add(reader.ReadByte());
}
字符串结果=Encoding.UTF8.GetString(bits.ToArray());
}

谢谢各位,我尝试了上述方法,但他们似乎总是出错。无论如何,我自己想出了一个循环,我在这里分享:

int c = 0;
int runs = 0;
byte[] data = new byte[400]; // byte array setup

// read hex values (loop) and convert to acsii string
BinaryReader reader = new BinaryReader(new FileStream("C:\File", FileMode.Open, FileAccess.Read, FileShare.None));
reader.BaseStream.Position = 0x1290;

while (c == 0)
{
    data[runs] = reader.ReadByte();
    if (data[runs] == 0x00)
    {
       c = 1; // stop loop at .ReadByte = 0x00
    }
    runs++;
}
reader.Close();

// hex to acsii string, removing null bytes
result = Encoding.Default.GetString(data.Where(x => x != 0).ToArray());

似乎效果不错

但问题是什么?如果需要读取大量字节直到达到0字节,为什么不能一次读取一个字节呢?你能帮我吗?我是c#的新手。我想我需要某种循环,但我不能使用byte[]数组?我还打算将十六进制字符串转换为文本字符串以显示,因为长度可以是任何东西,所以你说的[单词的长度]是什么意思?如果搜索到的单词有一个恒定的长度,就把它作为数组的长度,否则就把搜索到的单词的最大长度
编码。默认值
不是ASCII。如果需要ASCII,请使用
编码.ASCII
。如果没有,请找出您实际需要的编码,否则您将陷入麻烦。还请注意,您可以使用
break
退出循环。还有一种
bool
类型是“循环变量”的更好选择。和
Encoding.GetString
有一个参数表示要解码的字节数-try
Encoding.ASCII.GetString(数据,0,运行-1)取而代之。
int c = 0;
int runs = 0;
byte[] data = new byte[400]; // byte array setup

// read hex values (loop) and convert to acsii string
BinaryReader reader = new BinaryReader(new FileStream("C:\File", FileMode.Open, FileAccess.Read, FileShare.None));
reader.BaseStream.Position = 0x1290;

while (c == 0)
{
    data[runs] = reader.ReadByte();
    if (data[runs] == 0x00)
    {
       c = 1; // stop loop at .ReadByte = 0x00
    }
    runs++;
}
reader.Close();

// hex to acsii string, removing null bytes
result = Encoding.Default.GetString(data.Where(x => x != 0).ToArray());