如何从c#中的已知偏移量读取4、6、8字节?
文件偏移量|值:如何从c#中的已知偏移量读取4、6、8字节?,c#,C#,文件偏移量|值: 0x2274=0F 0x2276=63 我试图读取偏移量2274到2276;但是,它的读数不正确 //read Binary BinaryReader br = new BinaryReader(File.OpenRead(filepath)); //Name Change long nameoffset = Convert.ToInt64(0x2274, 16); br.BaseStream.Position = nameoffset; namevalues = br.Re
0x2274=0F
0x2276=63 我试图读取偏移量2274到2276;但是,它的读数不正确
//read Binary
BinaryReader br = new BinaryReader(File.OpenRead(filepath));
//Name Change
long nameoffset = Convert.ToInt64(0x2274, 16);
br.BaseStream.Position = nameoffset;
namevalues = br.ReadByte().ToString("X4");
string GetName = Convert.ToString("NameChg");
if (GetName == "NameChg")
{
long myvalue = Convert.ToInt64(namevalues, 16);
MessageBox.Show("Current Name: " + Convert.ToString(myvalue));
}
结果返回为“0015”。我需要将结果返回为“25359". 解决我自己的难题。我困惑了好几天。下面是我的代码示例,供任何希望这样做的人使用。如果你知道缩短这些代码的更好方法,请随意发布
//This is where you'll code to load the binary file.
var filepath = Path.Combine(Directory.GetCurrentDirectory(), "Test.bin");
//read Binary
BinaryReader br = new BinaryReader(File.OpenRead(filepath));
string namevalues = null;
//test offset purposes
for (int i = 0x2274; i <= 0x2276; i++)
{
br.BaseStream.Position = i;
namevalues += br.ReadByte().ToString("X2");
}
br.Close();
//put binary hex to array
string newStr = System.Text.RegularExpressions.Regex.Replace(Convert.ToString(namevalues), ".{2}", "$0,");
newStr = newStr.TrimEnd(',');
string[] NewBytes = newStr.Split(',').ToArray();
//Reverse order in Array
Array.Reverse(NewBytes);
newStr = NewBytes[0] + NewBytes[1] + NewBytes[2];
int decValue = Convert.ToInt32(newStr, 16);
String decValue2 = Convert.ToString(decValue);
//test output
messagebox.show(decValue2);
//您将在此处编写代码以加载二进制文件。
var filepath=Path.Combine(Directory.GetCurrentDirectory(),“Test.bin”);
//读取二进制文件
BinaryReader br=新的BinaryReader(File.OpenRead(filepath));
字符串namevalues=null;
//测试抵销目的
对于(int i=0x2274;i)您想读取2个字节,但您只读取了一个br.ReadByte()
。使用而不是读取整型文字0x2274
已经是“正确”的整数值8820
,您不需要在那里使用Convert
“
和Convert.ToString()
在那里调用?你正在将的“NameChg”
与的“NameChg”
进行比较,后者总是为真的
。将0x0F+0x63转换为9915没有什么意义。0x0F63==3939,0x630F==25359。首先解决这个问题。汉萨桑-你是正确的。实际答案应该是'25359' 十进制。我列出的偏移量也将被ini变量替换。Progman-名称CHG将被ini文件中的变量替换。我只需要知道如何读取4字节及以上的数据。