Binary reader

Lauris_Bruzgulis

New member
Joined
Sep 2, 2014
Messages
2
Programming Experience
Beginner
Question is how to read few offsets into text box For example i have binary file from which i need to read offsets in this order 0x234 0x235 0x230 0x231 which in hex decimal is 41 32 43 35 output should be 1325 and displayed into one text box language is c#
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,209
Location
Sydney, Australia
Programming Experience
10+
So, you're saying that you want to read four individual bytes from a file? If that's the case then you would use a BinaryReader at all. Just a simple FileStream. Call Seek or set Position and then call Read or ReadByte. If that's not what you want then please provide a FULL and CLEAR explanation.
 

Lauris_Bruzgulis

New member
Joined
Sep 2, 2014
Messages
2
Programming Experience
Beginner
Thanks for reply and sorry if something isnt clear as im just starting c# self learning
Yes i need to read 4 individual bytes from a file and display them in one textbox.
File is binary .bin extension, file size not larger than 512 bytes.
to be clear and with all details file has line
0x1E0 00 00 00 01 09 00 0F BB 36 30 35 39 FE 32 A5 53
bytes needed 36 30 35 39 (0x1E8, 0x1E9, 0x1EA, 0x1EB)
in order 36 35 30 39 (0x1E8, 0x1EA, 0x1E9 0x1EB)
and displayed into text box as 6509
Have tried to read as much info as i can for filestream, tried to make some but nothing worked and i cant find any code sample how to get it displayed into text box, now starting from 0 again and thats why im asking for some help, example etc
For now i have code using binary reader which works with some bugs but works
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
if (ofd.ShowDialog() == DialogResult.OK)
{
button2.Enabled = true;
path = ofd.FileName;
}
}


private void button2_Click(object sender, EventArgs e)
{
BinaryReader br = new BinaryReader(File.OpenRead(path));
br.BaseStream.Position = 0x234;
foreach (char myChar in br.ReadChars(4)) textBox1.Text += myChar;
br.BaseStream.Position = 0x1CE;
foreach (char myChar in br.ReadChars(17)) textBox2.Text += myChar;
br.BaseStream.Position = 0xA0;
foreach (char myChar in br.ReadChars(2)) textBox3.Text += myChar;
br.BaseStream.Position = 0x22;
foreach (char myChar in br.ReadChars(3)) textBox5.Text += myChar;
br.BaseStream.Position = 0x1CE;
foreach (char myChar in br.ReadChars(7)) textBox4.Text += myChar;
{
if (textBox4.Text == "W0L0AHL")
{
MessageBox.Show("H IPC");
}
else if (textBox4.Text == "W0L0AHM")
{
MessageBox.Show("B IPC");
}
{
if (textBox3.Text == "WC")
{
MessageBox.Show("PETROL");
}
}
}
}
}
}
have noticed that all works fine but when button2 is clicked again it duplicates all info in textboxies, tried br.dispose but it doesnt work or i incorectly used it
 
Top Bottom