Jump to content

BinaryReader in C#: getting data from offset X to end of file


Recommended Posts

Ok, I don't know if that's confusing or not, but I'll try to explain.  Basically I need to open a file that contains a jpg file inside a container.  The jpg file starts at offset E000 in the file, everything before that is header and information data for the rest of the file, but the JPG header starts at E000.

 

Here's what 've got as my code so far:

 

using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                //setup reader
                FileStream fs = new FileStream(ofd.FileName, FileMode.Open);
                BinaryReader br = new BinaryReader(fs);
                long length = fs.Length;
                //example read
                br.BaseStream.Position = 0x0000E000;
                byte[] example = br.ReadBytes(800);
                TextBox1.Text = BitConverter.ToString(example);
            }
        }
    }
}

 

Now, when I print out the byte array to the textbox using this method (at the bottom) I get a string of hex values with dashes between the bytes.  It reads all 800 values though.  When I use this method:

 

TextBox1.Text = Encoding.Default.GetString(example);

 

It prints out the characters up to the first 00 hex byte, not including that byte.  In the case of a JPEG header the first 00 byte is the 5th byte, meaning this only prints out the first 4.

 

I need to know how to make that not happen.  I plan on saving the file, so it needs to read the whole thing.

 

ALSO, I need to read from E000 to the end of the file and I can't for the life of me figure out how to go to the end of the file.  I've tried while and do until loops, but VB would let me just say do until results.length, C# won't let me do that since length is a LONG.

Link to comment
Share on other sites

The ReadBytes method reads the input number of bytes from the current position, yes?

 

 

If so, number of bytes until the end = total bytes - current byte location.

 

 

You know the current location since you set the location with the Position setting.

Link to comment
Share on other sites

I'm sure it's not difficult to convert a long to a string.  In C++ it's all of like 3 lines.

 

Of course you can do math on a long.  It's a numeric type.  (Isn't a long a 64 bit signed int in C#?)

 

 

By the way, why would you need to convert the length to a string?

 

 

 

http://msdn.microsoft.com/en-us/library/system.io.filestream_properties.aspx

 

 

It appears that the FileStream should have a .Length property, but obviously you know that since you're using it in your code.

 

 

So what exactly is your problem with filelength?

Link to comment
Share on other sites

You know what, I found an amazing writeup on the structure of the files I'm trying to read.  Turns out everything I need is already there, like the offsets, length of content, etc.  My only problem now is trying to get at that information as some of it is meant to be read as decimal and some as hex.  Like, I can find out where the content starts by using a little math.  If the 2 byte values starting at offset 0xC032 are equal to FFFF, then the list of contents starts at 0xC000, otherwise it starts at 0xD000, then I just use some math and from that I can figure out where the actual content starts and the length of it.  Usually the length of the content is the length of the file - the content start, meaning you're right that I need to find filelength and subtract the byte value for offset D000.  Now, what to do...

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.