Click here to Skip to main content
15,919,340 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi All,

I have to read data from a binary file at a specific location 0*FFF0 and store the data into a variable using c# .The binary file is being given as input .

Can anyone help me out ?????
Posted
Updated 18-Jul-11 2:12am
v2

1 solution

You could try using a file stream. Seek() to the position you want to start from and then Read() the bytes you need.

Edit: You mentioned to need only one byte. The code is updated for that need.

using (System.IO.FileStream stream = new System.IO.FileStream(filepath, System.IO.FileMode.Open))
{
    int startIndex = 0xFFF0;

    stream.Seek( startIndex);
    byte data = stream.ReadByte();

    stream.Close();
}
Error handling is left to the implementer, of course.
 
Share this answer
 
v2
Comments
Jefis 18-Jul-11 8:50am    
You need to seek to reading position.
stream.Seek(offset <...>)
and then Read.
lukeer 19-Jul-11 5:31am    
You could, but since FileStream.Read() supports an offset right away, you don't need to and you should do so.
Jefis 19-Jul-11 9:09am    
No it doesn't!.
offset: The byte offset in array at which the read bytes will be placed.
lukeer 20-Jul-11 5:19am    
Yes, Jefis, you are right.
I thought completely wrong about the index parameter. I will update my answer accordingly.
Thank you for correcting this mistake.
Member 7979279 19-Jul-11 7:32am    
Thanks sir , Here actually in detail what i have to do is :I have 1 byte at the address FFF0 and i have to read the byte bits-wise i.e i have to store the first 4 bits in a variable and the other 4 bits in another variable from that fff0 address byte .eg :fff0 : int v1=first 4 bits ,v2 = other 4 bits ;fff0 =00100011 ;v1 =0011(lower bits),v2=0010(upper bits).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900