C#,二进制读取器和#x27;拉开';二进制文件
我正在尝试使用binaryreader来“分离”和索引二进制文件,但我需要一些帮助。 到目前为止,我已经做到了:C#,二进制读取器和#x27;拉开';二进制文件,c#,binaryfiles,binary-data,C#,Binaryfiles,Binary Data,我正在尝试使用binaryreader来“分离”和索引二进制文件,但我需要一些帮助。 到目前为止,我已经做到了: using (BinaryReader b = new BinaryReader(File.Open(openRaw.FileName, FileMode.Open))) { int pos = 0; // 2A. // Use Ba
using (BinaryReader b = new BinaryReader(File.Open(openRaw.FileName, FileMode.Open)))
{
int pos = 0;
// 2A.
// Use BaseStream.
int length = (int)b.BaseStream.Length;
while (pos < length)
{
//Move on
pos += sizeof(int);
}
}
使用(BinaryReader b=新的BinaryReader(File.Open(openRaw.FileName,FileMode.Open)))
{
int pos=0;
//2A。
//使用BaseStream。
int length=(int)b.BaseStream.length;
while(pos
但现在最困难的部分开始了,至少对我来说是这样
该文件包含由169个字节组成的字节数组,每个字节之间用!(0x21)字符。
对于每个字节数组,我需要将其拆分为由16位和8位组成的多个值。
我确切地知道索引,它总是一样的。例如:索引0+1包含一个表示速度的U16小数值,索引16+17包含一个表示压力的S16小数值。以此类推,通过169字节的每个数组
我该怎么处理
编辑:
我没有这个:
FileInfo f = new FileInfo(openRaw.FileName);
double s1 = Convert.ToDouble(f.Length);
double s2 = s1 / 170;
double s3 = Math.Floor(s2);
double s4 = s3 * 170;
using (BinaryReader b = new BinaryReader(File.Open(openRaw.FileName, FileMode.Open), Encoding.ASCII))
{
int pos = 0;
// 2A.
// Use BaseStream.
while (pos < s4)
{
while (b.PeekChar() != -1)
{
if (b.ReadByte() != 0x21)
{
flag = 1;
}
temp = b.ReadBytes(2);
Array.Reverse(temp);
seconds = BitConverter.ToUInt16(temp, 0);
temp = b.ReadBytes(2);
Array.Reverse(temp);
PW1 = BitConverter.ToUInt16(temp, 0);
temp = b.ReadBytes(2);
Array.Reverse(temp);
PW2 = BitConverter.ToUInt16(temp, 0);
temp = b.ReadBytes(2);
Array.Reverse(temp);
RPM = BitConverter.ToUInt16(temp, 0);
temp = b.ReadBytes(2);
Array.Reverse(temp);
advance = BitConverter.ToInt16(temp, 0);
NN = b.ReadBytes(6);
temp = b.ReadBytes(2);
Array.Reverse(temp);
Baro = BitConverter.ToInt16(temp, 0);
temp = b.ReadBytes(2);
Array.Reverse(temp);
map = BitConverter.ToInt16(temp, 0);
temp = b.ReadBytes(2);
Array.Reverse(temp);
mat = BitConverter.ToInt16(temp, 0);
temp = b.ReadBytes(2);
Array.Reverse(temp);
clt = BitConverter.ToInt16(temp, 0);
temp = b.ReadBytes(2);
Array.Reverse(temp);
tps = BitConverter.ToInt16(temp, 0);
temp = b.ReadBytes(2);
Array.Reverse(temp);
bat = BitConverter.ToInt16(temp, 0);
temp = b.ReadBytes(2);
Array.Reverse(temp);
AFR = BitConverter.ToInt16(temp, 0);
stemp16 = b.ReadInt16();
temp = b.ReadBytes(2);
Array.Reverse(temp);
knock = BitConverter.ToInt16(temp, 0); ; //33
NN = b.ReadBytes(135);
table1.Rows.Add((seconds * 0.00390625), RPM, map, (tps * 0.1), ((mat - 320) * 0.05555), ((clt - 320) * 0.05555), (PW1 * 0.000666), (PW2 * 0.000666), (advance * 0.1), (knock * 0.1), (RPM/100), (Baro * 0.1), (AFR * 0.1), (bat * 0.1));
}
//Move on
pos = pos+= sizeof(int);
}
dataGridView1.DataSource = table1;
}
FileInfo f=newfileinfo(openRaw.FileName);
双s1=转换为双(f.长度);
双s2=s1/170;
双s3=数学楼层(s2);
双s4=s3*170;
使用(BinaryReader b=新的BinaryReader(File.Open(openRaw.FileName,FileMode.Open),Encoding.ASCII))
{
int pos=0;
//2A。
//使用BaseStream。
while(pos
只需按照文件中的顺序从读取器中读取值即可。例如:
ushort speed = b.ReadUInt16();
byte something = b.ReadByte();
sbyte someOther = b.ReadSByte();
short pressure = b.ReadInt16();
大多数数据类型的读取都有很多方法
循环流的示例:
while (b.PeekChar() != -1) {
if (b.ReadByte() != 0x21) {
// error - didn't find valid separator
}
ushort speed = b.ReadUInt16();
// and read another 167 bytes of data
}
好啊我不知道事情这么简单。在我这样做之前,我需要隔离每个字节之间的字节数组!性格如何在binaryreader中/使用binaryreader实现这一点?@Felix:你为什么认为你需要这样做?您可以这样做,但是您将有一个字节数组,您必须从中提取值。只需读取169个字节,然后读取作为分隔符的字节,然后重复,直到文件结尾。嗯,所以我必须创建一个循环,每次到达分隔符时都重新初始化读取?或者让它计算字节数?@Felix:您应该跟踪从文件中读取的字节数,以便在每次迭代中读取169个字节,然后使用分隔符。您不能查找文件中的每个0x21值,因为这也可能发生在169字节的数据中。例如,如果速度是289,则表示为两个字节0x21 0x01。我理解您的意思。我真的不确定这样一个循环应该是什么样子。此外,文件始终以分隔符开头。你能给我举个例子吗?